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

佐手、 提交于 2019-12-21 04:29:27

问题


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?


回答1:


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@me.com", 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@me.com" });
intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));



回答2:


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>



回答3:


This way works for me:

public static void sendEmailWithImages(Context context, String emailTo, String emailCC, String subject, String emailText, String type, List<String> filePaths) {
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType(type);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[]{emailCC});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    if(filePaths != null) {
        for (String file : filePaths) {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    }
    try {
        context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.send_email_using_message)));
    }catch (ActivityNotFoundException e) {
        //TODO
    }
}



回答4:


This way works for me:

PackageManager pm = getPackageManager();
  Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
  List<ResolveInfo> pkgAppsList = pm.queryIntentActivities(emailIntent, PackageManager.GET_RESOLVED_FILTER);
  List<LabeledIntent> intentList = new ArrayList<>();
  for(ResolveInfo resolveInfo : pkgAppsList){
    String packageName = resolveInfo.activityInfo.packageName;
    Intent intent = new Intent();
    intent.setPackage(packageName);
    intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{});
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intentList.add(new LabeledIntent(intent, packageName, resolveInfo.loadLabel(pm), resolveInfo.icon));
  }

  try {
    Intent chooser = Intent.createChooser(intentList.remove(intentList.size() - 1), "Отправка билета на почту");
    LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(chooser);
  } catch (Exception ex) {
    Toast.makeText(TicketsActivity.this, "У Вас не установлен почтовый клиент.", Toast.LENGTH_SHORT).show();
  }


来源:https://stackoverflow.com/questions/12741865/e-mail-attachment-through-intent-using-mailto-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!