1

So i am trying to use Intent.ACTION_SEND to share an image from my app.

This is the code I am using

Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, shareImgURI);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share images..."));

and the shareImgURI is content://com.android.providers.media.documents/document/image%3A298

It opens the Chooser but when I select an option nothing happens. Am i missing something?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pavlos
  • 547
  • 1
  • 9
  • 23

7 Answers7

5

Try this

Bitmap icon = mBitmap;
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_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Chandan kushwaha
  • 941
  • 6
  • 26
5

An old question but posting for future visitors.

Kotlin code to share an image.

You need to supply content//: type URI to fit to file sharing scheme of Android N and above.

val sharingIntent = Intent(android.content.Intent.ACTION_SEND)
        sharingIntent.type = "image/*"
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)

        context.startActivities(arrayOf(Intent.createChooser(sharingIntent, "Share with")))
Taseer
  • 3,432
  • 3
  • 16
  • 35
1

Change your intent to this:

 Intent shareIntent = new Intent(Intent.ACTION_SEND);
 shareIntent.setType("image/jepg");
 shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+shareImgURI));
 startActivity(Intent.createChooser(shareIntent, "Share image"));

And add this code in your activity's onCreate() method

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
A P
  • 2,131
  • 2
  • 24
  • 36
Sushin Pv
  • 1,826
  • 3
  • 22
  • 36
  • why use `Uri.parse("file://" + shareImgURI)` when `shareImgURI` is already of Type `Uri`? – pavlos Oct 25 '17 at 13:10
  • Then when you parse the `stringUri` to `Uri variable` add `file://` and the path should be absolute path to the image like `/storage/emulated/0/Pictures/image.jpg` @pavlos – Sushin Pv Oct 25 '17 at 13:14
1

It maybe late, but just find this answer on StackoverFlow, unfortunately don't remember whose answer was this. Tested on Lollipop, Marshmallow, Oreo -> working.

  Uri uri = FileProvider.getUriForFile(this, "com.example.provider", new File(photoPath));
        Intent share = ShareCompat.IntentBuilder.from(this)
                .setStream(uri) // uri from FileProvider
                .setType("text/html")
                .getIntent()
                .setAction(Intent.ACTION_SEND) //Change if needed
                .setDataAndType(uri, "image/*")
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(share, getString(R.string.share_image)));    
Za101
  • 451
  • 3
  • 6
0

Use the below code

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings( "deprecation" )
public static Intent shareImage(Context context, String pathToImage) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

shareIntent.setType("image/*");

// For a file in shared storage.  For data in private storage, use a ContentProvider.
Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;}
0
Bitmap icon = mBitmap;
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_STREAM, 
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

Try this it will work.

Ritwik
  • 45
  • 8
0
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.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;
 }
Sachin Yadav
  • 303
  • 4
  • 12