我正在尝试发送带有附件的电子邮件。内部存储中的文件,所以这是我的代码:
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/