在android中共享图像时出错

时间:2016-07-03 19:36:05

标签: java android image share

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

每当我为共享编码时,它都会给出异常“不支持文件格式”
这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

将可绘制图像存储到内部存储中并选择该图像。这种方式可以帮到你。

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