Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
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)));
}
like image 941
Simon Huckett Avatar asked Oct 06 '16 14:10

Simon Huckett


1 Answers

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.

like image 119
CommonsWare Avatar answered Oct 24 '22 21:10

CommonsWare