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

时间:2010-12-29 09:15:12

标签: android android-intent

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

email.putExtra(android.content.Intent.EXTRA_STREAM,...)仅添加最后一个uri。

我可以附加多个文件吗?我认为这可以通过使用Intent.ACTION_SEND_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..."));

提前致谢。

5 个答案:

答案 0 :(得分:50)

有效:

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

然后添加文件'uris:

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

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

希望有所帮助。

答案 1 :(得分:9)

你可以使用 putParcelableArrayListExtra方法 意图如下所示。代替 使用这样的:email.putExtra(Intent.EXTRA_STREAM, Uri.parse(uri));,你可以使用 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);

答案 2 :(得分:3)

为我工作

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

答案 3 :(得分:1)

这是完成工作的功能:)

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

答案 4 :(得分:0)

经过1天的工作,我终于可以将多个图像文件从\ sdcard \ accident \ folder附加到电子邮件客户端。为了附加多个文件,我必须将图像添加到ContentResolver,后者负责图库图像提供程序。 这是完整的代码---

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

所以Code的第一部分没有变化 - 但是更改是在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;
            } 

这工作正常,我可以将多个图像文件附加到模拟器默认电子邮件客户端,然后成功发送。