使用电子邮件发送带有多个附件的数据

时间:2020-04-24 10:23:13

标签: android android-intent

我有一个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类型的各种应用。

图像如下所示:

enter image description here

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

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

请指导我。

我想问一下,

Google文档显示了一些简单的示例,这在某种程度上很有用,但是如何真正深入地学习这些主题呢?

谢谢。

2 个答案:

答案 0 :(得分:0)

因此,在这里,我们将使用两个意图:selectorIntentemailIntentselectorIntent将使用emailIntent来显示可用的应用程序。代码:

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。

答案 1 :(得分:-1)

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

这样做:

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