Content URI passed in EXTRA_STREAM appears to “To:” email field

牧云@^-^@ 提交于 2020-01-01 04:36:45

问题


I am creating a file in the cache directory that I'd like to share with others (via Gmail / WhatsApp etc). I am able to do this using FileProvider, and it works OK for 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 the address(es) that I pass via Intent.EXTRA_EMAIL.

So the user is required to delete the bogus (Uri) email address before sending. Any idea how to prevent this from happening?

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)));
}

回答1:


Replace:

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

with:

intent.setType("image/jpeg");

Your problem is not EXTRA_STREAM, but rather that you are putting the Uri in the Intent's data facet.

Also, if your minSdkVersion is below 21, you will need to take some extra steps to ensure that clients can read the content, as the Intent flag is not applied to EXTRA_STREAM automatically on earlier versions of Android.



来源:https://stackoverflow.com/questions/39898556/content-uri-passed-in-extra-stream-appears-to-to-email-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!