How to Share Image + Text together using ACTION_SEND in android?
Asked Answered
C

16

65

I want to share Text + Image together using ACTION_SEND in android, I am using below code, I can share only Image but i can not share Text with it,

private Uri imageUri;
private Intent intent;

imageUri = Uri.parse("android.resource://" + getPackageName()+ "/drawable/" + "ic_launcher");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
startActivity(intent);

Any help on this ?

Cordage answered 2/12, 2013 at 16:35 Comment(2)
forum.xda-developers.com/showthread.php?t=2222703 'MMS' is separate protocol for sending text/image messages. Not great because it has lots of carrier imposed limits. You may want to check it out.Makassar
Did you fixed this issue? if fixed means share you idea.Modest
L
71

You can share plain text with the following code

String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

So your full code (your image + text) becomes

private Uri imageUri;
private Intent intent;
    
imageUri = Uri.parse("android.resource://" + getPackageName() +
    "/drawable/" + "ic_launcher");

intent = new Intent(Intent.ACTION_SEND);
// text
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of content
intent.setType("*/*");
//sending
startActivity(intent);

I just replaced image/* with */*

Update:

Uri imageUri = Uri.parse("android.resource://" + getPackageName() +
    "/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
Lynlyncean answered 2/12, 2013 at 16:41 Comment(10)
put setType before first putExtra and retry.Lynlyncean
umm, try to share/send it to another appLynlyncean
i tried to gamil, whatsapp and viber, among of them gmail and viber crashed :(Cordage
its not working, image not sharing to fb, for other(twitter,gmail) its workingGarrek
cuz fb needs its own implementation way, you need to use its sdk.Lynlyncean
Working with Whatsapp, Gmail and many other applications, Thanks. I badly needed this FLAG_GRANT_READ_URI_PERMISSION. Thanks again +1. Do note that I'm using just the Intent part from this with a screenshot bitmap.Eyas
in whatsapp i'm getting error "Sharing failed, please try again."Linetta
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
you, dear sir, is a life savour and the massiah himself in blood and fleshEndlong
try this https://mcmap.net/q/299142/-how-to-share-image-text-together-using-action_send-in-androidDermatosis
C
20

please have a look on this code worked for me to share an text and image together

Intent shareIntent;
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
    OutputStream out = null;
    File file=new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    path=file.getPath();
    Uri bmpUri = Uri.parse("file://"+path);
    shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
    shareIntent.setType("image/png");
    startActivity(Intent.createChooser(shareIntent,"Share with"));

Don't forget to give WRITE_EXTERNAL_STORAGE Permission

also in facebook it can only share the image because facebook is not allowing to share the text via intent

Chert answered 29/5, 2017 at 8:5 Comment(5)
This is the best answer so far, and the only one which is working for me all the others are not.Zinazinah
best answer. in this pageLinetta
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
Unfortunately, on Viber it's only sharing the image, not the text.Cupbearer
to share mage using public folder and write permission, you can use app personal foldersYoshieyoshiko
D
6

It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.

Google's document says:

The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:

http://developer.android.com/training/sharing/send.html

I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in

 Activity.getFilesDir()

cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.

Update1: The following is working code to share image with text in Android 4.4.

        Bitmap bm = BitmapFactory.decodeFile(file.getPath());
        intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
            + Constant.SHARE_URL);
        String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Share Image"));

The side effect is we add an image in the MediaStore.

Dahna answered 12/7, 2015 at 4:34 Comment(2)
If the share is finished, you can simply remove the image from MediaStore and nothing happend ;-)Harpsichord
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
W
4

try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.

void share() {
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_TEXT, "hello #test");

    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse("file:///sdcard/temporary_file.jpg"));            
    startActivity(Intent.createChooser(share, "Share Image"));
}
Widmer answered 17/7, 2015 at 6:59 Comment(2)
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
@AmanVerma FB Story and FB Messenger are definitely two different things.Crosscurrent
W
4

I have been looking for solution of this question for a while and found this one up and running, hope it helps.

private BitmapDrawable bitmapDrawable;
private Bitmap bitmap1;
//write this code in your share button or function

bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
bitmap1 = bitmapDrawable.getBitmap();
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
String shareText="Share image and text";
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
Wahlstrom answered 23/5, 2017 at 12:52 Comment(1)
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
A
4
 String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
        Uri bitmapUri = Uri.parse(bitmapPath);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
        intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());

        startActivity(Intent.createChooser(intent, "Share"));
Accumbent answered 18/9, 2019 at 11:42 Comment(0)
U
3
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
Underbred answered 26/5, 2018 at 7:55 Comment(1)
Chooser is showing duplicate Apps like FB messenger and FB messenger 'Your Story'Latish
S
2

Check the below code it worked for me

    Picasso.with(getApplicationContext()).load("image url path").into(new Target() {

           @Override

            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
                        "\n"+ "your share url or text ");
                i.setType("image/*");
                i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                context.startActivity(Intent.createChooser(i, "Share using"));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
        });


     private Uri getLocalBitmapUri(Bitmap bmp) {
      Uri bmpUri = null;
      try {
          File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}
Subtend answered 4/3, 2019 at 10:51 Comment(0)
J
2
private void shareImage(){

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);


    File f =  new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
    Intent shareIntent;


    try {
        FileOutputStream outputStream = new FileOutputStream(f);
        bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);

        outputStream.flush();
        outputStream.close();
        shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
        shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    }catch (Exception e){
        throw new RuntimeException(e);
    }
    startActivity(Intent.createChooser(shareIntent,"Share Picture"));
}
Johnathanjohnathon answered 7/10, 2020 at 11:7 Comment(1)
pay attention to StrictMode.setVmPolicy, it's required to avoid crashWindermere
A
1

To share a drawable image, the image has to be first saved in device's cache or external storage.

We check if "sharable_image.jpg" already exists in cache, if exists, the path is retrieved from existing file.

Else, the drawable image is saved in cache.

The saved image is then shared using intent.

private void share() {
    int sharableImage = R.drawable.person2;
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
    String path = getExternalCacheDir()+"/sharable_image.jpg";
    java.io.OutputStream out;
    java.io.File file = new java.io.File(path);

    if(!file.exists()) {
        try {
            out = new java.io.FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    path = file.getPath();

    Uri bmpUri = Uri.parse("file://" + path);

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent,"Share with"));
}
Accordion answered 10/7, 2019 at 9:44 Comment(0)
B
1

I tried this solution with Android X and works perfectly with whatsapp, telegram and gmail. This is my code:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "<<sharingText>>");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<<imageResource>>"));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
if (getView() != null && getView().getContext() != null &&       
    shareIntent.resolveActivity(getView().getContext().getPackageManager()) != null) {                 
    getView().getContext().startActivity(Intent.createChooser(shareIntent, null));
}
Britannia answered 24/5, 2021 at 12:59 Comment(0)
S
0

By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.

String[] recipient = {"your_email_here"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
Sphere answered 28/5, 2016 at 6:6 Comment(0)
D
0

You can this code it is working

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_image);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*" + "text/*");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(),
            b, "Title", null);
    Uri imageUri = Uri.parse(path);
    share.putExtra(Intent.EXTRA_STREAM, imageUri);
    share.putExtra(Intent.EXTRA_TEXT, "Hey I am Subhajit");
    startActivity(Intent.createChooser(share, "Share via"));
Dermatosis answered 1/2, 2022 at 9:35 Comment(0)
H
0

File Provider:

it is good to use FileProvider to share image to other app securely. it work on all version of android.

i tried it and works fine.

Link: https://developer.android.com/reference/androidx/core/content/FileProvider

Hooked answered 13/2, 2023 at 14:55 Comment(0)
M
0

Here How to Share Image + Text together using ACTION_SEND in android

  1. create a temporary file to store your image

     File cacheDir = context.getCacheDir();
     File file = new File(cacheDir, "my_image.png");  // give any name to your temp file with suffix same as your image file (.png)
     file.deleteOnExit();  // this will delete your temp file on teminate
     FileOutputStream fos = new FileOutputStream(file);
    
  2. create bitmap for your drawable and store it in your file

     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_abc); // your image name without suffix
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);  // remember to use the same format as your image type (PNG)
     fos.close();
    
  3. fetch uri from your temp file

     Uri uri = FileProvider.getUriForFile(context, "in.abc.def.fileProvider", file); 
     // where "in.abc.def.fileProvider" is the name of the authorities for your provider which is defined in AndroidManifest.xml
    

dummy reference for AndroidManifest.xml

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="in.abc.def.fileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
  1. create an intent to send text and image

     Intent sendIntent = new Intent();
     sendIntent.setAction(Intent.ACTION_SEND);
     sendIntent.setType("image/*");
     sendIntent.putExtra(Intent.EXTRA_TEXT, "some text");
     sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    
     sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     Intent shareIntent = Intent.createChooser(sendIntent, null);
     activity.startActivity(shareIntent);
    
Metaxylem answered 11/3, 2023 at 21:27 Comment(0)
C
0

After trying out different options

Directly sending an image using Resource ID didnt work. It shows empty file

imageUri = Uri.parse("android.resource://"  + context.packageName  +
        "/drawable/" + R.drawable.image);

Tried copying in to bitmap and then insert in to media images . This worked

bitmap =     BitmapFactory.decodeResource(context.getResources(),R.drawable.image)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
path =     MediaStore.Images.Media.insertImage(context.getContentResolver(),
bitmap, "Title", null);
imageUri =  Uri.parse(path);

val intent = Intent(Intent.ACTION_SEND).apply {
    type = "image/png"
    putExtra(Intent.EXTRA_STREAM, imageUri)
    putExtra(Intent.EXTRA_TEXT, "new image" )
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Codfish answered 2/6, 2024 at 17:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.