打开电子邮件与多个附件,同时限制选择器只电子邮件应用? [英] Opening an email with multiple attachments, while restricting the chooser to ONLY email apps?

查看:160
本文介绍了打开电子邮件与多个附件,同时限制选择器只电子邮件应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Android上带有多个附件的发送电子邮件,而不在选配有非电子邮件应用程序的最佳方法的?

What is the best way on Android to send an email with multiple attachments without having non-email apps in the chooser?

在发送电子邮件,我用来做这样的:

When sending emails, I used to do it like this:

final Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@test.com" });
...

不幸的是,信息/ RFC822不再行之有效的从选择器,如Evernote的,驱动器,以及其他各种应用程序过滤掉不需要的应用程序。

Unfortunately, "message/rfc822" no longer works well for filtering out undesired apps from the chooser, such as Evernote, Drive, and various other apps.

最近,我发现这个变通方法,单个附件的作品:

I recently found this workaround that works for single attachments:

sendEmailIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:?to=test@test.com&subject...");
sendEmailIntent.setData(data);  
...

不幸的是,这并不对多个附件工作。我试了一下,它崩溃的Gmail。 :•

Unfortunately, this doesn't work for multiple attachments. I tried it, and it crashes Gmail. :S

推荐答案

我终于找到了一个解决方案,尽管它仅适用于冰淇淋三明治MR1以上。关键是要首先建立你的意图使用ACTION_SEND_MULTIPLE:

I finally found a solution, albeit one that only works on Ice Cream Sandwich MR1 and above. The trick is to first build your intent using ACTION_SEND_MULTIPLE:

sendEmailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.com" });                
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
final ArrayList<Uri> uris = /* ... Your code to build the attachments. */
sendEmailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

要限制它只有电子邮件应用,添加此code:

To restrict it to email apps only, add this code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    sendEmailIntent.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it.
    final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO);
    Uri data = Uri.parse("mailto:?to=some@email.com");
    restrictIntent.setData(data);
    sendEmailIntent.setSelector(restrictIntent);
}

当您触发此意图与startActivity(),你现在只能看到电子邮件的应用程序列表中,如果你选择Gmail中,多个附件将在那里。

When you fire this intent with startActivity(), you'll now only see email apps in the list, and if you select Gmail, the multiple attachments will be there.

我这样做有一个try / catch万一startActivity解析到任何活动,在这种情况下,我删除了选择,而且它似乎运作良好。

I do this with a try/catch in case startActivity resolves to no activities, in which case I remove the selector, and it seems to work well.

这篇关于打开电子邮件与多个附件,同时限制选择器只电子邮件应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆