EXTRA_STREAM 中传递的内容 URI 出现在 "To:" 电子邮件字段中

Content URI passed in EXTRA_STREAM appears to "To:" email field

我正在缓存目录中创建一个文件,我想与其他人共享(通过 Gmail / WhatsApp 等)。我可以使用 FileProvider 执行此操作,并且它适用于 WhatsApp。 When choosing to share on Gmail the photo is correctly attached, but the Uri that I pass via Intent.EXTRA_STREAM also ends up being parsed by Gmail as an address in the "To:" field of the newly composed email, along with我通过 Intent.EXTRA_EMAIL.

传递的地址

因此要求用户在发送前删除伪造的(Uri)邮箱地址。知道如何防止这种情况发生吗?

Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.mypackage.fileprovider", cacheFile);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(contentUri, "image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"some_address@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
intent.putExtra(Intent.EXTRA_TEXT, "Check out this photo");
intent.putExtra(Intent.EXTRA_STREAM, contentUri);

if(intent.resolveActivity(getActivity().getPackageManager()) != null)
{
    startActivity(Intent.createChooser(intent, getString(R.string.share_file)));
}

替换:

intent.setDataAndType(contentUri, "image/jpeg");

与:

intent.setType("image/jpeg");

您的问题不是 EXTRA_STREAM,而是您将 Uri 放在 Intent 的数据方面。

此外,如果您的 minSdkVersion 低于 21,您将需要使用 some extra steps to ensure that clients can read the content,因为 Intent 标志不会自动应用于早期版本的 EXTRA_STREAM Android.