Simon Huckett Simon Huckett - 1 year ago 94
Android Question

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

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.putExtra(Intent.EXTRA_EMAIL, new String[] {""});
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)));

Answer Source


intent.setDataAndType(contentUri, "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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download