Share image with Android intent

不问归期 提交于 2019-12-06 00:16:59

First, there is no guarantee that any given other app will be able to support an android:resource// Uri. You will have greater compatibility sharing a file on external storage or using a ContentProvider.

That being said, replace:

File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));

with:

    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);

An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

BitmapDrawable bitmapDrawable = (BitmapDrawable)ImageView.getDrawable(); Bitmap bitmap = bitmapDrawable.getBitmap();

        // Save this bitmap to a file.
        File cache = getApplicationContext().getExternalCacheDir();
        File sharefile = new File(cache, "toshare.png");
        Log.d("share file type is", sharefile.getAbsolutePath());
        try {
            FileOutputStream out = new FileOutputStream(sharefile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e("ERROR", String.valueOf(e.getMessage()));

        }


        // Now send it out to share
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file://" + sharefile));

        startActivity(Intent.createChooser(share,
                "Share Image"));

In my case I used:

Uri imageUri = Uri.parse("android.resource://com.examle.tarea/" + R.drawable.tienda_musica);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);           
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);         
startActivity(Intent.createChooser(intent, getString(R.string.action_share))); 

//open a share intent chooser (Will show installed app from which i can share //images)

private void shareImage(String imagePath, String quoteByPerson, String quoteToShare) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        // String imagePath = Environment.getExternalStorageDirectory() +
        // "/myImage.png";
        File imageFileToShare = new File(imagePath);
        Uri uri = Uri.fromFile(imageFileToShare);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        share.putExtra(Intent.EXTRA_TEXT, "Quote of the day-:" + "\n" + quoteToShare + "\n" + quoteByPerson);

        share.putExtra(android.content.Intent.EXTRA_TITLE, "Quote of the day-:");

        if (imagePath.contains("android.resource://"))
        {
            Uri imageUri = Uri.parse(imagePath);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);

        } else
        {
            share.putExtra(Intent.EXTRA_STREAM, uri);
        }
        startActivity(Intent.createChooser(share, "Share inspiration via..."));
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!