Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share image with Android intent


I'm trying to share an image trough a share intent like this:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

    sharingIntent.setType("image/png");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, application.getString(R.string.app_name));
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,application.getString(R.string.app_share_message));

    File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
        shareMe(sharingIntent);

The share intent fires correctly and I choose Gmail, everything runs as expected until I press send. I receive a notification "Unable to show attach", and the e-mail is sent without it... Why?

Thanks for your time.

like image 417
GuilhE Avatar asked Jan 13 '23 21:01

GuilhE


1 Answers

First, there is no guarantee that any given other app will be able to support an android:resource// Uri. You will have greater compatibility sharing a file on external storage or using a ContentProvider.

That being said, replace:

File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));

with:

    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);

An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

like image 165
CommonsWare Avatar answered Jan 27 '23 06:01

CommonsWare