【问题标题】:Sending data with multiple attachments with email intent使用电子邮件意图发送带有多个附件的数据
【发布时间】:2020-08-07 21:30:44
【问题描述】:

我有一个 FeedbackActivity.java 活动,该活动从用户那里获取带有多个附件(最多 3 张图片作为附件)的反馈。

我正在使用以下代码:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);     
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails);                   //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject);                //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text)                       //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
                                                              //uris stores all Uri of images selected

if(intent.resolveActivity(getPackageManager()) != null){
    startActivity(intent);
}
else {
    Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}

现在这段代码可以正常工作,但问题是它显示了支持“message/rfc822”MIME 类型的各种应用程序。

图片如下:

我只需要显示电子邮件客户端应用程序,我尝试了 Uri.parse("mailto:"),但没有锻炼,代码总是移动到 else 语句并显示吐司“不好”。

我阅读了谷歌文档,但它只显示了简单的案例。 我尝试在网上搜索。许多开发人员正在使用intent.setType("*/*")intent.setType("text/plain")。但它们也都展示了电子邮件客户端以外的应用程序。

请指导我。

我想问一下,

谷歌文档展示了一些简单的例子,在某种程度上很好,但是如何真正深入地学习这些主题呢?

谢谢。

【问题讨论】:

  • 您可以尝试stackoverflow.com/a/59365539/115145 中为ACTION_SEND 显示的选择器和选择器方法——我还没有为ACTION_SEND_MULTIPLE 尝试过这个。
  • 是的,这真的有效! selectorIntent 带有操作 Intent.ACTION_SENDTO 和 emailIntent 带有操作 Intent.ACTION_SEND_MULTIPLE。像魅力一样工作!
  • 您可能希望使用最终使用的代码发布答案。很高兴听到它对您有用!
  • 我不明白为什么人们无缘无故地投反对票!这对 StackOverflow 来说真的很糟糕!对新手来说完全没有动力。

标签: android android-intent


【解决方案1】:

所以在这里,我们将使用两个意图:selectorIntentemailIntentselectorIntentemailIntent 将用来显示可用应用程序的内容。代码:

Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));

final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
    startActivity(emailIntent);
}
else {
    Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}

现在它将只选择电子邮件客户端的应用程序。

如果您的手机中只有一个电子邮件客户端应用程序,它将直接打开它。如果没有这样的应用程序,那么代码将显示在 else 部分中给出的 Snackbar。

【讨论】:

    【解决方案2】:

    不要使用 Uri.parse,使用 Uri.fromParts

    这样做:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example@mail.com", null));
    

    【讨论】:

    • 我想发送多个附件,所以 Intent.ACTION_SEND_MULTIPLE 是我需要的,而 Uri.fromParts() 在与 Intent.ACTION_SEND_MULTIPLE 一起使用时无法检测到电子邮件客户端
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多