22

I have this code:

 Intent intent = new Intent(); 
 intent.setAction(Intent.ACTION_SEND); 
 startActivity(intent); 

Which will successfully launch a Messaging App on Android.

But how can I attach a Bitmap object when launching the intent?

I have read http://developer.android.com/reference/Android/content/Intent.html, the closet thing to what I need is EXTRA_STREAM, like this:

intent2.putExtra(Intent.EXTRA_STREAM, _uri);

But my case, I have a reference of Bitmap object, not an URI of a Bitmap.

Please tell me what to do to attach a Bitmap object?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
KCRaju
  • 544
  • 3
  • 7
  • 21
  • you want to send image?? – Sagar Maiyad Jun 18 '13 at 04:35
  • Save that bitmap and provide the uri .... – Triode Jun 18 '13 at 04:37
  • @segi :: Yes i want to send image. – KCRaju Jun 18 '13 at 04:37
  • @Triode: i don't know the URI becoz i just taken tht image using dev cam – KCRaju Jun 18 '13 at 04:38
  • @KCRaju check this http://stackoverflow.com/questions/14457457/android-intent-send-an-email-with-image-attachment – Raghunandan Jun 18 '13 at 04:39
  • I have only following information about that image:: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); } } – KCRaju Jun 18 '13 at 04:39
  • 1
    Possible duplicate of [Sharing Bitmap via Android Intent](https://stackoverflow.com/questions/33222918/sharing-bitmap-via-android-intent) – tripleee Jun 19 '18 at 09:45

5 Answers5

30
    String pathofBmp = Images.Media.insertImage(getContentResolver(), bitmap,"title", null);
    Uri bmpUri = Uri.parse(pathofBmp);
    final Intent emailIntent1 = new Intent(     android.content.Intent.ACTION_SEND);
    emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
    emailIntent1.setType("image/png");

Where bitmap is your bitmap object which must be store in SD Card. and then use that Uri for shareimage.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
  • 3
    Images.Media.insertImage(getContentResolver(), bitmap,"title", null); return null – Ashish Dwivedi Aug 08 '14 at 07:00
  • @Riser this works perfectly as Intended, But since we are storing the image in SD Card. Is it just storing temporarily, Because if not, If user shares a lot of images, will this flood the SD Card ? and we should have some code to delete these. I guess these are temporary storing because i can't see these in my gallery. Just confirm ?Thankyou so much for the answer – Ashok Varma Feb 19 '15 at 02:48
  • 2
    @Riser i checked again those are stored permanently in pictures. This can Flood his SD card. is their any way we can specify the name of image. so we can replace new file with the old one. – Ashok Varma Feb 19 '15 at 03:55
  • 2
    This also requires `android.permission.WRITE_EXTERNAL_STORAGE`, or `grantUriPermission()` – Mygod Nov 19 '15 at 06:03
23

You must first save the bitmap to a file. you can save it to the app's cache

private void shareBitmap (Bitmap bitmap,String fileName) {
    try {
        File file = new File(getContext().getCacheDir(), fileName + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(     android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Gil SH
  • 3,789
  • 1
  • 27
  • 25
  • 1
    The most important in this example: `file.setReadable(true, false);` without that, other Apps (which you may be the result of startActivity) will not be able to read the image. All other answers neglected that.. – micha Dec 09 '16 at 15:14
  • how can we delete after sharing ?? – Prashanth Debbadwar Jun 05 '17 at 14:44
2

Try this it may help you:

ByteArrayOutputStream bos = new ByteArrayOutputStream();  
yourbitmapimagename.compress(CompressFormat.PNG, 0, bos);
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
intent.setType("*/*"); 
intent.putExtra(Intent.EXTRA_STREAM, bos.toByteArray());
startActivity(intent); 
Wilfredo P
  • 4,070
  • 28
  • 46
Nas
  • 2,158
  • 1
  • 21
  • 38
0

@Gil Sh's answer is a great starting point. However, at least for newer Android versions (API >=24), it doesn't work out of the box. It will throw an Exception:

android.os.FileUriExposedException: file.png exposed beyond app through ClipData.Item.getUri()

To get around that, you need to use a FileProvider. There is a good answer in a different thread showcasing this.

However, take care if you used the internal cache (i.e. via getContext().getCacheDir()) to store the image as suggested in @Gil Sh's answer. Instead of this:

Then you need to add a file named filepaths.xml to your app/src/main/res/xml directory with the following contents:

<paths>
   <external-files-path name="Pictures" path="Pictures" />
</paths>

Use this:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache" path="."/>
</paths>

The nice thing about using the cache folder is that you won't spam the users Gallery or other folders with the images. Also, you won't need additional Permissions for your App.

mc51
  • 1,883
  • 14
  • 28
-12
 String cc=trlink.toString();
 Intent share = new Intent(Intent.ACTION_SEND);
 share.setType("text/plain");
 share.putExtra(Intent.EXTRA_TEXT,cc);
 startActivity(Intent.createChooser(share,"Share Text"));
Triode
  • 11,309
  • 2
  • 38
  • 48
haresh
  • 321
  • 2
  • 3