0

I have picture in drawable and I want to share that one image.

Following is my code,

 btnic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.defaultpicture);
            Intent ppp = new Intent("android.intent.action.SEND");
            ppp.setType("image/png");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    b, "defaultpicture", null);
            Uri uri =  Uri.parse(path);
            ppp.putExtras(Intent.EXTRA_STREAM, uri); **i am getting error here**
            MainActivity.this.startActivity(Intent.createChooser(ppp, "Send picture"));
            Toast.makeText(getApplicationContext(), "Picture Copied", Toast.LENGTH_SHORT).show();
        }
    });

where am i doing wrong?

Ravi Kilnake
  • 175
  • 2
  • 14

2 Answers2

4

Intent.EXTRA_STREAM takes value as Local path of file on storage not on resource (Not a resource ID). So you need to first save the image as file in storage then pass the url to sharing Intent . First decode resource.

Bitmap b = BitmapFactory.decodeResource(res, id);

Save it to specific location on storage and use this file path as value in intent. you can save it as below or in some other way.

String path = MediaStore.Images.Media.insertImage(getContentResolver(),
        b, "myFile", null);
ADM
  • 20,406
  • 11
  • 52
  • 83
  • Error:(55, 20) error: no suitable method found for putExtras(String,Uri) method Intent.putExtras(Intent) is not applicable (actual and formal argument lists differ in length) method Intent.putExtras(Bundle) is not applicable (actual and formal argument lists differ in length).. i am confused – Ravi Kilnake Feb 16 '18 at 14:07
0
                final Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("image/jpg");
                final File photoFile = new File(getFilesDir(), "yourimage.jpg");
                Log.d("TAG", "onClick: "+photoFile);
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
                startActivity(Intent.createChooser(shareIntent, "Share image using"));

for more about Sharing Content with Intents