I am using Intent .ACTION_SEND
to get default email clien开发者_运维技巧t. It works fine but now i need to attach more than one file to email.
email.putExtra(android.content.Intent.EXTRA_STREAM,...)
attaches only last uri added to it.
So can I attach multiple files? I think this can be done by using Intent.ACTION_SEND_MULTIPLE
. Here is the code I am trying:
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..."));
Thanks in advance.
That works:
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");
then add files' uris:
ArrayList<Uri> uris = new ArrayList<Uri>();
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
You can use
putParcelableArrayListExtra
method of
Intent as shown below. Instead of
using like this: email.putExtra(Intent.EXTRA_STREAM, Uri.parse(uri));
, you can use an
ArrayList
as shown below:
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);
Worked For Me
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..."));
Here is function that will do the job :)
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;
}
}
After 1 day work finally I am able to attach multiple image files from \sdcard\accident\ folder to email client. For attaching multiple files I had to add the images to the ContentResolver which is responsible for gallery images provider. Here is the Complete Code ---
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:"));
So there is no change in the First Section of Code -- But Change is in getUriListForImages() method which is as follows---
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;
}
This is working fine and I am able to attach multiple image files to emulator default email client and send then successfully .
精彩评论