我正在使用此代码附加文件:
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..."));
(请注意,path
类似于“/sdcard/test.jpg”,我使用ACTION_SENDTO
,因为我只想在选择器中看到电子邮件应用。)
意图将提供电子邮件应用程序列表,但附件不会出现在电子邮件或Gmail中。如何才能显示附件?
答案 0 :(得分:2)
这似乎适用于我的Galaxy Nexus和Nexus 4(两者都运行JellyBean API 17)。
具体做法是:
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..."));
这不适用于我的Nexus One(Gingerbread API 10)或更旧的设备。我不确定它在什么时候开始工作。
也许别人对此有更多细节?
当ACTION_SENDTO不合适时:
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));
答案 1 :(得分:1)
意图将提供电子邮件应用程序列表,但是 附件未显示在电子邮件或Gmail中。我怎么能得到 附件显示?
我多年来一直在寻找这个问题的解决方案,为什么在某些设备或Android版本或邮件应用中,Intent.ACTION_SENDTO
适合通过putExtra(Intent.EXTRA_STREAM,Uri.fromFile(...))
传输附件信息,而在其他设备上不是。
答案很简单:它取决于相应邮件应用程序的清单。大多数邮件应用在android:mimeType
的意图过滤器中没有android.intent.action.SENDTO
。
使Intent.ACTION_SENDTO
通常与附件一起使用的唯一解决方案是告诉所有邮件应用程序制造商他们应该扩展其清单(和相应的代码),如下所示:
<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>
答案 2 :(得分:0)
这种方式对我有用:
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
}
}
答案 3 :(得分:0)
这种方式对我有用:
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();
}