如何在android中将多个文件附加到电子邮件客户端

如何在android中将多个文件附加到电子邮件客户端,android,android-intent,Android,Android Intent,我正在使用Intent.ACTION\u SEND获取默认电子邮件客户端。它工作正常,但现在我需要附加多个文件到电子邮件 email.putExtraandroid.content.Intent.EXTRA_-STREAM,。。。只附加最后添加到它的uri 那么我可以附加多个文件吗?我认为这可以通过使用Intent.ACTION\u SEND\u MULTIPLE来实现。以下是我正在尝试的代码: String uri=getScreenShot(); Intent email = new In

我正在使用Intent.ACTION\u SEND获取默认电子邮件客户端。它工作正常,但现在我需要附加多个文件到电子邮件

email.putExtraandroid.content.Intent.EXTRA_-STREAM,。。。只附加最后添加到它的uri

那么我可以附加多个文件吗?我认为这可以通过使用Intent.ACTION\u SEND\u MULTIPLE来实现。以下是我正在尝试的代码:

String uri=getScreenShot();

Intent email = new Intent(android.content.Intent.ACTION_SEND);
            email.setType("application/octet-stream");
            email.putExtra(Intent.EXTRA_STREAM, Uri.parse(uri));
            email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file:"+csvpath));
            alert.dismiss();
            ctx.startActivity(Intent.createChooser(email, "Send mail..."));
提前感谢。

您可以使用 putParcelableArrayListExtra法 意图如下所示。而不是 像这样使用:email.putExtraIntent.EXTRA_STREAM,Uri.parseuri;,你可以使用 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);
}
email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
这是有效的:

final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
然后添加文件的URI:

ArrayList<Uri> uris = new ArrayList<Uri>();

ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

希望对您有所帮助。

经过一天的工作,我终于能够将\sdcard\contract\文件夹中的多个图像文件附加到电子邮件客户端。为了附加多个文件,我必须将图像添加到ContentResolver中,ContentResolver负责gallery图像提供程序。 这是完整的代码--

Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("plain/text");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"soubhabpathak2010@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Accident Capture");
sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

ArrayList<Uri> uriList = getUriListForImages();
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
Log.d(TAG, "Size of the ArrayList :: " +uriList.size());
FormHolderActivity.this.startActivity(Intent.createChooser(sendIntent, "Email:"));
因此,在代码的第一部分中没有变化,但在getUriListForImages方法中有变化,如下所示--



    private ArrayList<Uri> getUriListForImages() throws Exception {
                ArrayList<Uri> myList = new ArrayList<Uri>();
                String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/accident/";
                File imageDirectory = new File(imageDirectoryPath);
                String[] fileList = imageDirectory.list();
                if(fileList.length != 0) {
                    for(int i=0; i<fileList.length; i++)
                    {   
                        try 
                        {   
                            ContentValues values = new ContentValues(7);
                            values.put(Images.Media.TITLE, fileList[i]);
                            values.put(Images.Media.DISPLAY_NAME, fileList[i]);
                            values.put(Images.Media.DATE_TAKEN, new Date().getTime());
                            values.put(Images.Media.MIME_TYPE, "image/jpeg");
                            values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode());
                            values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileList[i]);
                            values.put("_data", imageDirectoryPath + fileList[i]);
                            ContentResolver contentResolver = getApplicationContext().getContentResolver();
                            Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                            myList.add(uri);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                return myList;
            } 


这工作正常,我能够将多个图像文件附加到emulator默认电子邮件客户端并成功发送。

以下是完成此工作的函数:

public static void sendEmailMulipleFiles(Context context, String toAddress, String subject, String body, ArrayList<String> attachmentPath) throws Exception {
    try {
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { toAddress });
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);
        intent.setType("message/rfc822");
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
        ResolveInfo best = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.contains(".gm.") || info.activityInfo.name.toLowerCase().contains("gmail"))
                best = info;
        }
        ArrayList<Uri> uri = new ArrayList<Uri>();
        for (int i = 0; i < attachmentPath.size(); i++) {
            File file = new File(attachmentPath.get(i));
            uri.add(Uri.fromFile(file));
        }

        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uri);


        if (best != null)
            intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);

        context.startActivity(Intent.createChooser(intent, "Choose an email application..."));
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}
为我工作

 Intent emailIntent=new Intent(Intent.ACTION_SEND_MULTIPLE, Uri.parse("mailto:"+ clientEmail));
                emailIntent.putExtra(Intent.EXTRA_SUBJECT,"working-tutor-form From App");
                emailIntent.setType("text/plain");
                Uri uri1 = Uri.parse("file://" +  URI1);
                Uri uri2 = Uri.parse("file://" +  URI2);
                Uri uri3 = Uri.parse("file://" +  URI3);
                ArrayList<Uri> arrayList=new ArrayList<Uri>();
                arrayList.add(uri1);
                arrayList.add(uri2);
                arrayList.add(uri3);
                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,arrayList);

                emailIntent.putExtra(Intent.EXTRA_TEXT,body);
                startActivity(Intent.createChooser(emailIntent,"Send Via..."));

是的,我这样做了,但它在打开gmail客户端时给出了nullpointerexception。打开默认电子邮件客户端时,它不会显示任何附件。请注意,如果您从gmail发送带有多个附件和类似垃圾邮件的文本的邮件,您的google帐户可能会被禁用。尝试此操作时请尝试使用开发帐户。此帐户仅在第一次运行。当我同时发送2封邮件的时候,它只运行1次。ActhOnsEnthyMulk执行这项工作:我正在从我的资产文件夹中获取图像。请帮助我如何使用这行URI Tururi=URI.PAR获得多个图像。secontent://com.jamia.binoria/+fatwaImageArray;我可以需要获取所有图像并将其放入ArrayList吗?ACTION\u SEND\u MULTIPLE对我不起作用。我只在代码中将其从ACTION_SENDTO更改为,当intent菜单打开时,显示没有应用程序可以执行此操作。这里列出的任何其他代码都是在电子邮件中发送多个文件所必需的吗?伙计!你做到了!我把时间浪费在很多不工作的代码上!你的代码终于开始工作了!谢谢!