Share image with Android intent

13,016

Solution 1

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.

Solution 2

BitmapDrawable bitmapDrawable = (BitmapDrawable)ImageView.getDrawable(); Bitmap bitmap = bitmapDrawable.getBitmap();

        // Save this bitmap to a file.
        File cache = getApplicationContext().getExternalCacheDir();
        File sharefile = new File(cache, "toshare.png");
        Log.d("share file type is", sharefile.getAbsolutePath());
        try {
            FileOutputStream out = new FileOutputStream(sharefile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e("ERROR", String.valueOf(e.getMessage()));

        }


        // Now send it out to share
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file://" + sharefile));

        startActivity(Intent.createChooser(share,
                "Share Image"));

Solution 3

In my case I used:

Uri imageUri = Uri.parse("android.resource://com.examle.tarea/" + R.drawable.tienda_musica);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);           
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);         
startActivity(Intent.createChooser(intent, getString(R.string.action_share))); 
Share:
13,016
GuilhE
Author by

GuilhE

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian W. Kernighan

Updated on June 04, 2022

Comments

  • GuilhE
    GuilhE almost 2 years


    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.