android:将文件附加到邮件

时间:2012-04-17 05:56:40

标签: android android-intent email-attachments

我有这个发送邮件的代码:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MyMail@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
     startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(BladeActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

如何将此文件附加到此邮件:/sdcard/MyFile.csv

感谢,

3 个答案:

答案 0 :(得分:6)

试试这个:

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/MyFile.csv"));

并确保您具有访问外部存储所需的权限。

答案 1 :(得分:1)

以下是在邮件中附加文件的代码...当我尝试发送邮件时,此代码有效..

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of Email");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("/mnt/sdcard/MyFile.csv"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the mail");
startActivity(Intent.createChooser(sendIntent, "Email:"));     

在manifest.xml文件中添加以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 2 :(得分:1)

使用此代码..

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+arr));
intent.setType("image/jpg"); 
startActivity(intent);