WELCOME

WELCOME TO TECH-VILLA.

SEARCH YOUR TECHNICAL STUFF

OR

BROWSE THROUGH LABELS IN SIDEBAR

HAPPY BLOGGING.

Sunday, March 6, 2011

android- how to invoke email client in android and add multiple attachments to it.

This is very common task in android to invoke email client from your application. there are many tutorial available out there about how to invoke email client in apps and add attachment to email client....


but i faced the real prob when i had to attach multiple attachments in email. i had searched a lot and finally implemented it correctly.


So here is a short tutorial on how to add multiple attachments to email client in android


you will have to start with a Intent with Intent.ACTION_SEND_MULTIPLE
 //here is the intent
final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
//set the type of data
//application/octet-stream gives only email options. messaging and bluetooth will not show up in the application chooser
ei.setType("application/octet-stream");
// set the email address, you want in "To" field
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
// add subject
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
 
Now here comes the main part which will attach the multiple attachments in email
 
first we will make an array list of Uris...as follows .....
ArrayList<Uri> uris = new ArrayList<Uri>();
 suppose u have to attach two files then take there locations and parse them to Uris....File fileIn1 = new File(filepath1);
        Uri u1 = Uri.fromFile(fileIn1);
File fileIn2 = new File(filepath2);
        Uri u2 = Uri.fromFile(fileIn2);
 now we will add these Uri to array list
 
uris.add(u1);
uris.add(u2);
 // add these uris to intent.
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
 
and just start the activity..... 
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
 
So here's the complete code....
 
final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("application/octet-stream");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
 
ArrayList<Uri> uris = new ArrayList<Uri>();

File fileIn1 = new File(filepath1);
        Uri u1 = Uri.fromFile(fileIn1);
File fileIn2 = new File(filepath2);
        Uri u2 = Uri.fromFile(fileIn2);
uris.add(u1);
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
 
So done with the multiple attachments in the email clien from android app....