java - 如何发送带有 android Intent 的附件的电子邮件?

标签 java android file email storage

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

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

我该如何解决??

最佳答案

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

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..."));

关于java - 如何发送带有 android Intent 的附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38234297/

相关文章:

java - 使用 Android 执行 RESTful URL

java - 从二维数组转置矩阵

Java 和 Python 在单个 Google App Engine 项目中一起使用

java - 在不同屏幕尺寸上运行应用程序时,谷歌地图标记会变得更大

Javascript将CSV文件加载到数组中

java - 如何在java中制作分层Json

android - 仅在 firebase 调用完成时加载布局

Android:单例实例与服务

iOS:如何写入项目中特定目录的文件?

node.js - 如何使用 node.js 加密将公钥和私钥保存到文件?