Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android multiple email attachments using Intent

I've been working on Android program to send email with an attachment (image file, audio file, etc) using Intent with ACTION_SEND. The program is working when email has a single attachment. I used Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is working fine, the mail can be delivered through the Gmail. However, when I tried to have multiple images attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.

I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constant ACTION_SEND_MULTIPLE (available since API level 4) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else, it works like ACTION_SEND, except the data is multiple. But I still could not figure out the correct usage for this command. I tried to declare intent with ACTION_SEND_MULTIPLE, then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images, but I got the same erroneous result just like before, none of the attachment show up in the email.

Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment?

like image 269
yyyy1234 Avatar asked Feb 15 '10 08:02

yyyy1234


1 Answers

Here is the code you need to create an emailIntent that contains multiple attachments.

public static void email(Context context, String emailTo, String emailCC,     String subject, String emailText, List<String> filePaths) {     //need to "send multiple" to get more than one attachment     final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);     emailIntent.setType("text/plain");     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,          new String[]{emailTo});     emailIntent.putExtra(android.content.Intent.EXTRA_CC,          new String[]{emailCC});     emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);      emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);     //has to be an 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);     }     emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);     context.startActivity(Intent.createChooser(emailIntent, "Send mail...")); } 
like image 133
gregm Avatar answered Sep 21 '22 11:09

gregm