使用电子邮件意图发送带有多个附件的数据
Sending data with multiple attachments with email intent
我有一个 FeedbackActivity.java
activity,它从带有多个附件(最多 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 语句并显示 toast "not good".
我阅读了 google 文档,但它只显示了简单的案例。
我试着在网上搜索。许多开发人员正在使用 intent.setType("*/*")
或 intent.setType("text/plain")
。但它们也都显示电子邮件客户端以外的应用程序。
请指导我。
我想问一下,
Google 文档显示了简单的例子,这在某种程度上是好的,但是如何真正深入地学习这些主题呢?
谢谢。
不要使用Uri.parse,使用Uri.fromParts
这样做:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example@mail.com", null));
所以在这里,我们将使用两个意图:selectorIntent
和 emailIntent
。 selectorIntent
是 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();
}
现在它将只选择电子邮件客户端应用程序。
如果您的 phone 中只有一个电子邮件客户端应用程序,它会直接打开它。如果没有这样的应用程序,那么代码将显示 else 部分中给出的 Snackbar。
我有一个 FeedbackActivity.java
activity,它从带有多个附件(最多 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 语句并显示 toast "not good".
我阅读了 google 文档,但它只显示了简单的案例。
我试着在网上搜索。许多开发人员正在使用 intent.setType("*/*")
或 intent.setType("text/plain")
。但它们也都显示电子邮件客户端以外的应用程序。
请指导我。
我想问一下,
Google 文档显示了简单的例子,这在某种程度上是好的,但是如何真正深入地学习这些主题呢?
谢谢。
不要使用Uri.parse,使用Uri.fromParts
这样做:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example@mail.com", null));
所以在这里,我们将使用两个意图:selectorIntent
和 emailIntent
。 selectorIntent
是 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();
}
现在它将只选择电子邮件客户端应用程序。
如果您的 phone 中只有一个电子邮件客户端应用程序,它会直接打开它。如果没有这样的应用程序,那么代码将显示 else 部分中给出的 Snackbar。