使用意图的 Android 多个电子邮件附件

我一直致力于 Android 程序发送电子邮件与附件(图像文件,音频文件等)使用意图与 ACTION_SEND。当电子邮件只有一个附件时,程序正常工作。我使用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)附加指定的图像文件到邮件,它工作正常,邮件可以通过 Gmail 发送。但是,当我试图通过多次调用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)将多个图像附加到同一邮件时,它失败了。邮件中没有任何附件。

我搜索了有关电子邮件附件的 SDK 文档和 Android 编程用户组,但找不到任何相关信息。然而,我发现还有另一个意图常量 ACTION_SEND_MULTIPLE(自 API 级别4以来可用)可能满足我的需求。基于 SDK 文档,它只是简单地声明它将多个数据交付给其他人,它的工作方式类似于 ACTION_SEND,只不过数据是多个的。但是我仍然不知道这个命令的正确用法。我尝试用 ACTION_SEND_MULTIPLE声明意图,然后多次调用 putExtra(EXTRA_STREAM, uri)附加多个图像,但我得到了同样的错误结果,就像以前一样,没有一个附件显示在电子邮件中。

有没有人尝试使用 ACTION_SEND_MULTIPLE,并得到它与多个电子邮件附件的工作?

71375 次浏览

Here I found great example http://www.blackmoonit.com/2010/02/filebrowser-send-receive-intents/

you must use

final Intent aIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
aIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,theUris);
aIntent.setType(theOverallMIMEtype);

Here is the code you need to create an emailIntent that contains multiple attachments.

public static void email(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths)
{
//need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{emailCC});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

ACTION_SEND_MULTIPLE should be the action

and then emailIntent.setType("text/plain");

followed by:

ArrayList<Uri> uris = new ArrayList<Uri>();
String[] filePaths = new String[] {"sdcard/sample.png", "sdcard/sample.png"};
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);

This works for me.

Although this is an old thread, but as it is shown on top on google searches i want to add a small hint to make it complete, hence I stumpled upon it.

It is necessary to make the attached files readable for the mail activity, otherwise they will not be attached. So you have to call somewhere

fileIn.setReadable(true, false)

For multiple attachments use PutParcelableArrayListExtra(Intent.ExtraStream, uris)where uris variable is a List<IParcelable>(). Here's an example:

var email = new Intent(Intent.ActionSendMultiple);
email.SetType("text/plain");
email.PutExtra(Intent.ExtraEmail, new string[]{emailTo});
email.PutExtra(Intent.ExtraCc, new string[]{emailCC});


var uris = new List<IParcelable>();
filePaths.ForEach(file=> {
var fileIn = new File(file);
var uri = Android.Net.Uri.FromFile(fileIn);
uris.Add(uri);
});


email.PutParcelableArrayListExtra(Intent.ExtraStream, uris);


context.StartActivity(Intent.CreateChooser(email, "Send mail..."));

Hope this helps ;)