简体   繁体   中英

How to share image of app

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM