How to attach a Bitmap when launching ACTION_SEND intent
Asked Answered
D

5

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?

Demonic answered 18/6, 2013 at 4:34 Comment(7)
you want to send image??Hultgren
Save that bitmap and provide the uri ....Richman
@segi :: Yes i want to send image.Demonic
@Triode: i don't know the URI becoz i just taken tht image using dev camDemonic
@Demonic check this #14457957Handgrip
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); } }Demonic
Possible duplicate of Sharing Bitmap via Android IntentPortraitist
H
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.

Hultgren answered 18/6, 2013 at 4:39 Comment(4)
Images.Media.insertImage(getContentResolver(), bitmap,"title", null); return nullCensorship
@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 answerMarchak
@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.Marchak
This also requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()Elated
Y
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();
    }

}
Yasmin answered 28/9, 2015 at 17:57 Comment(2)
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..Basidium
how can we delete after sharing ??Counterplot
D
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); 
Debug answered 18/6, 2013 at 7:21 Comment(2)
Throws a ClassCastException: "byte[] cannot be cast to android.os.Parcelable"Nimmons
I think you have to import java.io.ByteArrayOutputStream to your class.Debug
B
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.

Banff answered 25/6, 2022 at 14:39 Comment(0)
T
-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"));
Troostite answered 18/6, 2013 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.