Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

E-mail attachment through intent using `mailto:` scheme

I'm using this code to attach a file:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
String uriText;
Uri file = Uri.fromFile(new File(path));
uriText = "mailto:" + 
              "?subject=the subject" + 
              "&body=the body of the message"+
              "&attachment="+file;
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
emailIntent.setData(uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

(Note that path is something like "/sdcard/test.jpg" and that I used ACTION_SENDTO because I just want to see e-mail apps in the chooser.)

The intent will provide a list of e-mail applications, but the attachment doesn't appear in Email or Gmail. How can I get the attachment to display?

like image 534
Frank Junior Avatar asked Oct 05 '12 07:10

Frank Junior


2 Answers

This seems to work on my Galaxy Nexus and Nexus 4 (both running stock JellyBean API 17).

Specifically:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "[email protected]", null));
intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
startActivity(Intent.createChooser(intent, "Send email..."));

This does NOT work on my Nexus One (Gingerbread API 10) or older devices. I'm not sure at what point it started working.

Maybe someone else has some more details on this?

When ACTION_SENDTO is not appropriate:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("vnd.android.cursor.dir/email");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
like image 79
Mark Avatar answered Nov 02 '22 10:11

Mark


The intent will provide a list of e-mail applications, but the attachment doesn't appear in Email or Gmail. How can I get the attachment to display?

I was seeking for years for a solution of this problem why it seems that on some devices or Android versions or mail apps the Intent.ACTION_SENDTO is suitable to transfer the attachment informations via putExtra(Intent.EXTRA_STREAM,Uri.fromFile(...)) while on others it is not.

The answer is quite simple: It depends on the Manifest of the respective mail app. Most mail apps don't have android:mimeType in the intent filter of android.intent.action.SENDTO.

The only solution to make Intent.ACTION_SENDTO generally work with attachments is to tell all makers of mail apps that they should extend their manifest (and the corresponding code) like so:

<intent-filter>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="mailto" />
    <data android:mimeType="text/plain" />
    <data android:mimeType="image/*" />
    <data android:mimeType="video/*" />
    ...
</intent-filter>
like image 29
red symbol man Avatar answered Nov 02 '22 11:11

red symbol man