使用 Intent 的 Android 多个电子邮件附件 [英] Android multiple email attachments using Intent

查看:34
本文介绍了使用 Intent 的 Android 多个电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发 Android 程序,使用 ACTION_SEND 使用 Intent 发送带有附件(图像文件、音频文件等)的电子邮件.当电子邮件只有一个附件时,该程序正在运行.我使用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) 将指定的图像文件附加到邮件中,并且工作正常,邮件可以通过 Gmail 投递.但是,当我尝试通过多次调用 Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) 将多个图像附加到同一邮件时,它无法正常工作.电子邮件中没有显示任何附件.

I've been working on Android program to send email with an attachment (image file, audio file, etc) using Intent with ACTION_SEND. The program is working when email has a single attachment. I used Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is working fine, the mail can be delivered through the Gmail. However, when I tried to have multiple images attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.

我在 SDK 文档和 Android 编程用户组中搜索了有关电子邮件附件的信息,但找不到任何相关信息.但是,我发现还有另一个意图常量 ACTION_SEND_MULTIPLE(自 API 级别 4 起可用)可能满足我的要求.根据 SDK 文档,它只是声明它向其他人提供多个数据,它的工作原理类似于 ACTION_SEND,只是数据是多个.但是我仍然无法弄清楚这个命令的正确用法.我试图用 ACTION_SEND_MULTIPLE 声明意图,然后多次调用 putExtra(EXTRA_STREAM, uri) 来附加多个图像,但我得到了和以前一样的错误结果,没有附件显示在电子邮件中.

I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constant ACTION_SEND_MULTIPLE (available since API level 4) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else, it works like ACTION_SEND, except the data is multiple. But I still could not figure out the correct usage for this command. I tried to declare intent with ACTION_SEND_MULTIPLE, then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images, but I got the same erroneous result just like before, none of the attachment show up in the email.

有没有人尝试过 ACTION_SEND_MULTIPLE 并让它与多个电子邮件附件一起工作?

Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment?

推荐答案

这是创建包含多个附件的 emailIntent 所需的代码.

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..."));
}

这篇关于使用 Intent 的 Android 多个电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆