How to use "Share image using" sharing Intent to share images in android? How to use "Share image using" sharing Intent to share images in android? android android

How to use "Share image using" sharing Intent to share images in android?


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


The solution proposed from superM worked for me for a long time, but lately I tested it on 4.2 (HTC One) and it stopped working there. I am aware that this is a workaround, but it was the only one which worked for me with all devices and versions.

According to the documentation, developers are asked to "use the system MediaStore" to send binary content. This, however, has the (dis-)advantage, that the media content will be saved permanently on the device.

If this is an option for you, you might want to grant permission WRITE_EXTERNAL_STORAGE and use the system-wide MediaStore.

Bitmap icon = mBitmap;Intent share = new Intent(Intent.ACTION_SEND);share.setType("image/jpeg");ContentValues values = new ContentValues();values.put(Images.Media.TITLE, "title");values.put(Images.Media.MIME_TYPE, "image/jpeg");Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,        values);OutputStream outstream;try {    outstream = getContentResolver().openOutputStream(uri);    icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);    outstream.close();} catch (Exception e) {    System.err.println(e.toString());}share.putExtra(Intent.EXTRA_STREAM, uri);startActivity(Intent.createChooser(share, "Share Image"));


First add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

using Bitmap from resources

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage);Intent share = new Intent(Intent.ACTION_SEND);share.setType("image/jpeg");ByteArrayOutputStream bytes = new ByteArrayOutputStream();b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);Uri imageUri =  Uri.parse(path);share.putExtra(Intent.EXTRA_STREAM, imageUri);startActivity(Intent.createChooser(share, "Select"));

Tested via bluetooth and other messengers