31

I am using Intent .ACTION_SEND to get default email client. 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.

ismail
  • 46,010
  • 9
  • 86
  • 95
N-JOY
  • 10,344
  • 7
  • 51
  • 69

5 Answers5

55

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);
starball
  • 20,030
  • 7
  • 43
  • 238
Herr K
  • 1,751
  • 3
  • 16
  • 24
  • Note that there is a risk of getting your google account disabled if you send mails with multiple attachments and spam-like text, from gmail. Try to use a development account when trying this. – Aswin Kumar Mar 08 '13 at 13:00
  • It is working only for the first time. When i sending the 2 mail simultaneously it works only for 1 time. – Deepak Goel Jan 24 '14 at 06:09
  • 5
    ACTION_SEND_MULTIPLE does the job :) – Calvin Mar 11 '14 at 12:02
  • i am getting images from my assets folder can u please help me how will i get multiple images using this line Uri theUri = Uri.parse("content://com.jamia.binoria/"+fatwaImageArray); may i need to get all images and put it in ArrayList? – user3233280 May 28 '14 at 18:15
  • 1
    ACTION_SEND_MULTIPLE did not work for me. I change only that in my code (from ACTION_SENDTO), and when the intent menu opens up it says "No apps can perform this action." Is any of the other code listed here necessary to send muliple file in an email? – OKGimmeMoney Jul 11 '14 at 16:38
  • It works only if I use this: emailIntent.setType("message/rfc822"); – Vinay Dec 04 '16 at 17:14
9

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);
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • 5
    yup i did this but it gives nullpointerexception on opening gmail client. upon opening up the default email client it does not show any attachment. – N-JOY Dec 29 '10 at 11:26
4

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..."));
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
2

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;
    }
}
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
0

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 .

Soubhab Pathak
  • 619
  • 7
  • 14