Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No image extension when sharing using an intent?

I use the following code in order to share an image :

 Uri imguri=Uri.parse("android.resource://PackageName/drawable/"+ R.drawable.img);
 Intent intent=new Intent(Intent.ACTION_SEND);
 intent.setType("image/png");
 intent.putExtra(Intent.EXTRA_STREAM,imguri);
 Intent ch=Intent.createChooser(intent,"Send image");
 startActivity(ch);

The file is sent (for instance using GMAIL) but the problem is that there is no extension. It is simply named 2130837559.

Is there a way to get the real name (img) and the extension (png) ?

like image 354
WildThing Avatar asked Nov 01 '22 07:11

WildThing


1 Answers

Partly, that is up to the email client, not you.

Partly, that is because your code does not send the real name or the extension. R.drawable.img is an int, quite possibly 2130837559 in decimal. If you look at your Uri that you are sending, you will not see the filename or the file extension.

Your only hope for getting Gmail to think in terms of your filename and extension will be to have a Uri that has those values in it. Solutions include:

  • Copying the resource to a file on external storage and using a file:// Uri (not recommended)

  • Copying the resource to a file on internal storage and using FileProvider to give you a content:// Uri

  • Using my StreamProvider to serve the value straight out of raw resources, and setting up the name to give your resulting Uri a filename and extension (note: this would require you to have the PNG file as a raw resource, rather than as a drawable resource)

like image 194
CommonsWare Avatar answered Nov 12 '22 17:11

CommonsWare