Share drawable image to other apps properly (sharing PNG to Whatsapp fails without FileProvider)

Questions:

I’m creating an Android app where the user can select one of several images to share (which are stored in the drawable folder) and the app opens up a standard ACTION_SEND chooser to allow them to share it to any app that supports PNGs, like so:

Uri imageUri = Uri.parse("android.resource://com.owlswipe.imagesharer/" + getImage());

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/png");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sendIntent, "share to an app"));

public int getImage() {
return R.drawable.firstimage;
}

However, if the user selects to share it to Whatsapp, it doesn’t work properly: instead of interpreting it as an image (like if you normally share a photo to Whatsapp) it thinks it’s a document called “Untitled” and does not show as an image.

Whatsapp problem

Opening this Untitled document on a computer reveals it’s called DOC-20180721-WA0012. with no file extension! Manually adding a png to the end of the filename reveals the correct image.

Making this weirder (but definitely solvable somehow!):

  • If the user chooses to open the image in an SMS app, for example, the image appears normally.

  • This has happened with multiple devices (Pixel 2 on P beta and Nokia 2 on 7.1.1)

  • This issue does not happen with other apps, where PNGs can be sent via Whatsapp like a normal image (though they do seem to be automatically converted to JPEGs by Whatsapp).


What can I do to make sure Whatsapp sees my image as a proper PNG file? Alternatively, how do I properly share a pre-loaded image from my app such that every app can interpret it correctly?

Answers:



Need Help? Chat with us