在 android 中共享图像时出错

Error with Sharing Images in android

在 android 中,我无法共享 jpeg 和 png 格式的图像。请帮助并更正我的代码

每当我为分享编写代码时,它都会给出异常 "File format not supported"
这是我的代码:

Uri imageUri = Uri.parse("android.resource://com.parekh.shareimage/drawable");
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/*");
    startActivity(shareIntent);

将您的可绘制图像存储到您的内部存储器中,然后 select 该图像。这个方法对你有帮助。

try{

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.shareforma);
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File file = new File(extStorageDirectory, "forma.PNG");
        FileOutputStream outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    String msgText = "Sample Message";

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.setType("image/*");

    //set your message
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 

    String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";

    File imageFileToShare = new File(imagePath);

    Uri uri = Uri.fromFile(imageFileToShare);

    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(shareIntent, msgText));