5


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.

GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • 1
    @PrafulBhatnagar: " one application can not access the the local resource of another application" -- that is completely incorrect. Resources are readable by all applications on the device. – CommonsWare Mar 22 '13 at 12:13
  • @CommonsWare need help here http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 05:56
  • what is shareme here – Aditya Aug 06 '15 at 07:50
  • possible duplicate of [How to use "Share image using" sharing Intent to share images in android?](http://stackoverflow.com/questions/7661875/how-to-use-share-image-using-sharing-intent-to-share-images-in-android) – vianna77 Sep 06 '15 at 19:20

4 Answers4

6

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That was my first attempt and it worked, but when I receive the email the attachment Mimie-type is application/octet-stream instead of image/png – GuilhE Mar 22 '13 at 12:21
  • 1
    @GuilhE: Then do not use `android:resource://` `Uri` values. Use a file or a `ContentProvider`. – CommonsWare Mar 22 '13 at 12:55
  • That was my second approach and it's the answer ;) Just create a Bitmap decoded from the resources save it in the sdcard and use that file. Thanks! – GuilhE Mar 22 '13 at 15:21
  • @CommonsWare I've written a similar code which sends a image via gmail perfectly. I also want to be able to share the image on Facebook but Facebook icon doesn't appear on share window. Can you please see [this link](http://stackoverflow.com/questions/18031587/facebook-doesnt-appear-on-share-windows-when-trying-to-share-an-image)? – Alireza Farahani Aug 03 '13 at 10:45
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"));
Sai Aditya
  • 2,353
  • 1
  • 14
  • 16
1

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))); 
Hernaldo Gonzalez
  • 1,977
  • 1
  • 21
  • 32
0

//open a share intent chooser (Will show installed app from which i can share //images)

private void shareImage(String imagePath, String quoteByPerson, String quoteToShare) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        // String imagePath = Environment.getExternalStorageDirectory() +
        // "/myImage.png";
        File imageFileToShare = new File(imagePath);
        Uri uri = Uri.fromFile(imageFileToShare);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        share.putExtra(Intent.EXTRA_TEXT, "Quote of the day-:" + "\n" + quoteToShare + "\n" + quoteByPerson);

        share.putExtra(android.content.Intent.EXTRA_TITLE, "Quote of the day-:");

        if (imagePath.contains("android.resource://"))
        {
            Uri imageUri = Uri.parse(imagePath);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);

        } else
        {
            share.putExtra(Intent.EXTRA_STREAM, uri);
        }
        startActivity(Intent.createChooser(share, "Share inspiration via..."));
    }
DeepakPanwar
  • 1,389
  • 14
  • 22