我需要在电子邮件中附加一些文件,但在调用intent时出错。
如果我使用Intent.ACTION_SENDTO,选择器将返回Gmail应用程序和通用电子邮件客户端。但是,如果我尝试将Intent.ACTION_SEND_MULTIPLE作为参数传递,则不会得到能够接收该意图的应用程序。我该如何解决此问题?
这是我正在使用的代码。
ArrayList<Uri> attachments = new ArrayList<>();
File path = new File(Environment.getExternalStorageDirectory() + "appFolder");
if (path.exists()){
for (File child : path.listFiles()) {
attachments.add(Uri.fromFile(child));
}
//Intent intent = new Intent(Intent.ACTION_SENDTO);//This works, but I can't attach several files.
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);//This is the part that SHOULD work, but doesn't...
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
try {
startActivity(Intent.createChooser(intent, "Send Email"));
} catch (ActivityNotFoundException anfe) {
anfe.printStackTrace();
}
}
从去掉intent.setData(Uri.parse("mailto:"));
开始。ACTION_SEND_MULTIPLE
不使用setData()
。
然后,去掉intent.putExtra(Intent.EXTRA_TEXT, "");
。不要求任何ACTION_SEND_*
同时遵守EXTRA_STREAM
和EXTRA_TEXT
。由于你没有使用文本,如果你删除多余的文本,你会得到更可靠的结果。如果您坚持保留它,则需要使用put...ArrayListExtra()
,而不是putExtra()
,因为ACTION_SEND_MULTIPLE
需要一个数组,而不是单个值。
即使在这些变化之后,你仍然可能得不到任何活动。很少有应用程序实现ACTION_SEND_MULTIPLE
,只有其中的一个子集支持message/rfc822
。