-2

I want share image from on click of button

Java code

public void share(View v)
{
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    Uri a=Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.pic); 
    Log.i("imageUri",""+imageUri);
    share.putExtra(Intent.EXTRA_STREAM,a);
    startActivity(Intent.createChooser(share,"Share Image"));
}

by this code is not working , what changes should i do ?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Possible duplicate of [How to use "Share image using" sharing Intent to share images in android?](https://stackoverflow.com/questions/7661875/how-to-use-share-image-using-sharing-intent-to-share-images-in-android) – AskNilesh Oct 14 '17 at 09:40

2 Answers2

1

Try this,

First You need to add permission WRITE_EXTERNAL_STORAGE

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

Note : for Marshmallow and above version You need Runtime Permission of WRITE_EXTERNAL_STORAGE and Here is good example of Runtime Permission for EXTERNAL_STORAGE

Then use following code to share your Image

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.pic);
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 a=  Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, a);
startActivity(Intent.createChooser(share, "Select"));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
0

Use this code to share image:

NOTE: you have to add WRITE/READ EXTERNAL STORAGE permission in Menifest file to do this:

           Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    mBitmap, "Image Description", null);
            Uri uri = Uri.parse(path);

            Intent intent = new Intent(Intent.ACTION_SEND);
            Log.d("imageUri", "imageUriIs" + uri);
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.putExtra(Intent.EXTRA_SUBJECT, "");
            intent.putExtra(Intent.EXTRA_TEXT, shareMSG);
            intent.putExtra(Intent.EXTRA_TITLE, "TITLE");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setType("image/*");
            startActivity(Intent.createChooser(intent, "APPNAME"));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30