[英]How can I send an email with an attached file with android's intent?
我正在嘗試發送附帶附件的電子郵件。 內部存儲中的文件,所以這是我的代碼:
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..."));
- 確保已在清單中添加了讀取權限
uses-permission android:name =“android.permission.READ_EXTERNAL_STORAGE”。
-GMail 5.0僅接受來自外部存儲的文件Gmail 5.0應用程序在收到ACTION_SEND意圖時失敗並顯示“附件權限被拒絕” 。
你也可以使用這個庫:compile'com.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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.