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

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

提前致谢。

这样可行:

 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 uris = new ArrayList(); ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345); 

希望有所帮助。

您可以使用Intent的putParcelableArrayListExtra方法,如下所示。 而不是像这样使用: email.putExtra(Intent.EXTRA_STREAM, Uri.parse(uri)); ,您可以使用ArrayList ,如下所示:

 ArrayList uris = new ArrayList(); //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); 

为我工作

  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 arrayList=new ArrayList(); 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...")); 

这是可以完成工作的function:)

 public static void sendEmailMulipleFiles(Context context, String toAddress, String subject, String body, ArrayList 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 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 = new ArrayList(); 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; } } 

经过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 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 getUriListForImages() throws Exception { ArrayList myList = new ArrayList(); String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/accident/"; File imageDirectory = new File(imageDirectoryPath); String[] fileList = imageDirectory.list(); if(fileList.length != 0) { for(int i=0; i 

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