3

I'm having this issue where sharing an image from my app to Gmail puts the path of the image in the To field.

Here's the code that I'm using:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Beam Dental Insurance Card");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(insuranceCardImageUri, getActivity().getContentResolver().getType(insuranceCardImageUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, insuranceCardImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Insurance Card"));

And here's what I get.

enter image description here

The To: field gets filled in with the path to the image with the "content:" removed from the front. I've tried setting the EXTRA_EMAIL on the intent but that doesn't help.

CaseyB
  • 24,780
  • 14
  • 77
  • 112

2 Answers2

6

First, replace:

shareIntent.setDataAndType(insuranceCardImageUri, getActivity().getContentResolver().getType(insuranceCardImageUri));

with:

shareIntent.setType(getActivity().getContentResolver().getType(insuranceCardImageUri));

as ACTION_SEND does not use a Uri in the data field of the Intent.

Then, remove:

shareIntent.setType("image/*");

as you do not need to call setType() twice (or even call setType() and setDataAndType(), as you have it here).

Also, bear in mind:

  • If the Uri is not coming from your app (e.g., your own ContentProvider), third-party apps like Gmail may not be able to use it, as they may not have permission to access it. This is not significantly different than passing a URL to a third-party app, where the URL requires an authenticated user session to be useful.

  • There is no requirement for ACTION_SEND implementations to honor both EXTRA_STREAM andEXTRA_TEXT`.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You can share image using share intent, but you've to decode image to a localized Bitmap

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);

intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));

loadedImage is the loaded bitmap from http://eofdreams.com/data_images/dreams/face/face-03.jpg

acoording to Nitin Misra

Community
  • 1
  • 1
Nirel
  • 1,855
  • 1
  • 15
  • 26