5

On Android I try to share image + text like this :

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"));

this work fine on Whatsapp (for example) but on viber it's show only the image and skip the text :( On Instagram it's the total opposite, text is send but not the image ...

How can i do ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • @MartinBraun: First, `EXTRA_STREAM` is supposed to hold a `Uri` with a `content` scheme, not `android.resource`. Few apps will be set up to handle the obscure `android.resource` scheme. Use a `ContentProvider`, such as `FileProvider`, to serve your content to share. Second, the MIME type is supposed to represent the MIME type of the stream; a drawable resource is not a JPEG. Finally, sharing *both* text *and* a stream goes beyond the `ACTION_SEND` contract, so there is no guarantee that any given app will support both in a single share operation. – CommonsWare Oct 18 '21 at 11:36
  • @CommonsWare Yes I know that `EXTRA_STREAM` requires an `Uri` instead of the image itself. However, we can use a `FileOutputStream`, `flush()` and `close()` it and then use `FileProvider.getUriForFile()` to get the `Uri` as you said. So, I am aware of that, but I recognize that there might be limitations regarding the send contract and its mime type, I'm still hoping for a possible solution to share image (uri) + text at once. I've seen attempts to use the mime type `*/*` without success. Can't believe Rakuten and Facebook devs think so limited. – Martin Braun Oct 19 '21 at 19:23

1 Answers1

1

Try this for viber :

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setPackage("com.viber.voip");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Message");

Checkout this answer for the Instagram part : https://stackoverflow.com/a/16299999

Hope this helps !!

Anonymous
  • 2,184
  • 15
  • 23
  • 1
    Have you read the question at all?:) The problem described is that Viber ignores the text when it's shared together with image and your solution about sharing text only (without image). – Roman Pavlov Sep 09 '21 at 17:05
  • There is no benefit in sharing an image and text directly to Viber using `setPackage`, because the user should be free in choosing where to share image + text after all. – Martin Braun Oct 18 '21 at 08:52