how to attach multiple files to email client in android
Asked Answered
W

5

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.

Warnke answered 29/12, 2010 at 9:15 Comment(0)
S
55

That works:

final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
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);
Stromberg answered 29/12, 2010 at 11:33 Comment(6)
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.Reeder
It is working only for the first time. When i sending the 2 mail simultaneously it works only for 1 time.Laos
ACTION_SEND_MULTIPLE does the job :)Bluebell
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?Centrepiece
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?Biliary
It works only if I use this: emailIntent.setType("message/rfc822");Matter
P
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);
Press answered 29/12, 2010 at 10:41 Comment(3)
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.Warnke
@TalhaÇ Did you try the accepted answer? Probably that will workWarnke
@Warnke didn't work for my case.Angelineangelique
C
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..."));
Crigger answered 3/4, 2017 at 5:38 Comment(0)
P
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;
    }
}
Primordial answered 10/10, 2013 at 7:1 Comment(1)
Man! You did! i wasted time with lots of not working codes! Finally your code is working! Thanks a loT!Norward
C
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[] {"[email protected]"});
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 .

Columella answered 15/7, 2011 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.