我一直在开发 Android 程序,以使用带有 ACTION_SEND
的 Intent 发送带有附件(图像文件、音频文件等)的电子邮件。当电子邮件只有一个附件时,该程序正在运行。我使用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)
将指定的图像文件附加到邮件中,它工作正常,邮件可以通过 Gmail 传递。但是,当我尝试通过多次调用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)
将多个图像附加到同一封邮件时,它无法正常工作。电子邮件中未显示任何附件。
我搜索了有关电子邮件附件的 SDK 文档和 Android 编程用户组,但找不到任何相关信息。但是,我发现还有另一个 Intent 常量 ACTION_SEND_MULTIPLE
(从 API 级别 4 开始可用)可能满足我的要求。根据 SDK 文档,它只是声明它向其他人传递多个数据,它的工作方式类似于 ACTION_SEND
,除了数据是多个。但我仍然无法弄清楚这个命令的正确用法。我尝试用 ACTION_SEND_MULTIPLE
声明 Intent ,然后多次调用 putExtra(EXTRA_STREAM, uri)
以附加多个图像,但我得到了和以前一样的错误结果,没有一个附件显示在电子邮件中。
有没有人尝试使用 ACTION_SEND_MULTIPLE
并让它与多个电子邮件附件一起使用?
最佳答案
这是创建包含多个附件的 emailIntent 所需的代码。
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..."));
}
关于使用 Intent 的 Android 多个电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2264622/