3

I have a share feature which should share both image and text together. I am doing it as shown below

val uri = FileProvider.getUriForFile(context!!,BuildConfig.APPLICATION_ID + ".provider",file)

val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.type = "*/*"

if (etShareMessage.text.isNotEmpty()) {
    intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text)
}

val title = resources.getString(R.string.screenshot_share_with)

val chooser = Intent.createChooser(intent, title)

// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(activity?.packageManager) != null) {
    startActivity(chooser)
} else {
    ToastUtils.showToast(context,getString(R.string.screenshot_sorry_no_apps_to_share))
    dismiss()
}

It works fine for Gmail and Hike. But WhatsApp shows only the image. I have referred this post and few other links. But no luck. I can see that Amazon app and Loco app are able to do this.

Why isn't WhatsApp taking text from the above intent?

starball
  • 20,030
  • 7
  • 43
  • 238
Annah
  • 306
  • 3
  • 11

3 Answers3

1
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

                    Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
                    String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bm, "", null);
                    Uri screenshotUri = Uri.parse(path);

                    intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                    intent.setType("image/*");
                    startActivity(Intent.createChooser(intent, "Share image via..."));
                } else {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }

By this method we can share image to whatsapp the image is in an imageView

Jinson Paul
  • 481
  • 6
  • 17
0

Try

Intent.ACTION_SEND_MULTIPLE

instead of:

Intent.ACTION_SEND
Sahil
  • 952
  • 1
  • 7
  • 14
0

After doing a lot of trial and error, I found that the way we pass text via Intent.EXTRA_TEXT matters for WhatsApp!!

I just changed

intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text)

to

intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text.toString())

and it worked.

Annah
  • 306
  • 3
  • 11