使用 Intent 的 Android 多个电子邮件附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2264622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:12:27  来源:igfitidea点击:

Android multiple email attachments using Intent

androidemailandroid-intentattachment

提问by yyyy1234

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.

我一直在开发 Android 程序,使用带有ACTION_SEND. 当电子邮件只有一个附件时,该程序正在运行。我Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)以前把指定的图片文件附加到邮件中,效果很好,邮件可以通过Gmail投递。但是,当我尝试通过Intent.putExtra(android.content.Intent.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.

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

Has anyone tried with ACTION_SEND_MULTIPLEand got it working with multiple email attachment?

有没有人尝试过ACTION_SEND_MULTIPLE并使用多个电子邮件附件?

回答by gregm

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

这是创建包含多个附件的 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..."));
}

回答by santhana

ACTION_SEND_MULTIPLEshould be the action

ACTION_SEND_MULTIPLE应该是行动

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

进而 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.

这对我有用。

回答by thomas

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)

回答by printminion

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

在这里我找到了很好的例子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);

回答by Michele Caggiano

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

对于多个附件使用PutParcelableArrayListExtra(Intent.ExtraStream, uris)where uris variable is aList<IParcelable>().这是一个例子:

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 ;)

希望这可以帮助 ;)