TJ1
TJ1

Reputation: 8498

Sharing an image with some text under it, both in the same message

I am looking to add a text message under an image (not printed on the image) so when I click a share button, the image, and text message to be shared as one message that looks like this:

enter image description here

My code to share an image looks like this:

public void shareImage(View view) {
    Intent shareIntent = new Intent();
    Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}

How can I add the text Image created by: abcd.com under the image, (as a text not printed on the image) in the same text message or email etc.

Upvotes: 1

Views: 468

Answers (3)

A simple case of sharing a bitmap to Twitter/Whatsapp/Facebook Messenger was resolved thanks to @Aniruddh Chandratre solution. MediaStore.Images.Media.insertImage returns a String in content://media format i.e. content://media/external/images/media/610

val savedImageURI = MediaStore.Images.Media.insertImage(
         activity.contentResolver, bitmap, "title", "decription")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(savedImageURI))
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(Intent.createChooser(shareIntent, "Share Image"))

Upvotes: 0

Shubham Vala
Shubham Vala

Reputation: 1043

Try this

    Uri imgUri = Uri.fromFile(new File(DIRECTORY + "/" + fileName));

    //Add this code if you get SecurityException error
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Text you want to attach with image.");
    startActivity(Intent.createChooser(shareIntent, "Share Image"));

Upvotes: 1

Aniruddh Chandratre
Aniruddh Chandratre

Reputation: 474

The following snippet of code should work. We add the image to MediaStore.

public void shareImage(View view) {
    Intent shareIntent = new Intent();
    shareIntent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
        + Constant.SHARE_URL);
    Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
    Bitmap bm = BitmapFactory.decodeFile(photoURI);
    String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}

Upvotes: 1

Related Questions