【问题标题】:How can I send an email with an attached file with android's intent?如何发送带有 android 意图的附件的电子邮件?
【发布时间】:2016-07-06 21:44:57
【问题描述】:

我正在尝试发送带有附件的电子邮件。内部存储中的文件,所以这是我的代码:

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));

但我总是得到:Permission denied for file

我该如何解决??

【问题讨论】:

标签: java android file email storage


【解决方案1】:

我是这样解决的:我将要发送的文件复制到外部缓存目录中,然后发送。

File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(keyType.getKeyTypeString(), ".pem", context.getExternalCacheDir() );
        Utils.copy(new File(getFilesDir().getAbsolutePath()+"/"+ Utils.APP_OPERATOR_DIR, keyType.getKeyTypeString()+".pem"), temporaryFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));

【讨论】:

    【解决方案2】:

    -确保您已在清单中添加读取权限

    使用权限 android:name="android.permission.READ_EXTERNAL_STORAGE"。

    -GMail 5.0 只接受来自外部存储的文件 Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent.

    你也可以使用这个库:compile'c​​om.github.yesidlazaro:GmailBackground:1.1'。

        String imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH);
    
        BackgroundMail.newBuilder(ReportBugActivity.this)
                .withUsername("some_email@gmail.com")
                .withPassword("pages123")
                .withMailto("mail_to_email.bugs@gmail.com")
                .withSubject("Android Bug Report")
                .withAttachments(imagePath)
                .withBody("Android Bug Report")
                .withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();
    
                        finish();
                        startActivity(getIntent());
                    }
                })
                .withOnFailCallback(new BackgroundMail.OnFailCallback() {
                    @Override
                    public void onFail() {
                        Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
                    }
                }).send();
    

    【讨论】:

    • 但我的文件不在外部存储中,而是在内部存储中
    • 你有读取内部存储的权限吗? android.permission.READ_INTERNAL_STORAGE
    • 我用过这个库:compile 'com.github.yesidlazaro:GmailBackground:1.1'。而且使用起来非常简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多