PNG的共享选项

时间:2017-02-16 16:56:37

标签: java android bitmap png share

我正在尝试在我的应用中为png图片实现共享选项,但我收到了TransactionTooLargeException。我做了什么:我添加了压缩我的Bitmap的代码,但我仍然得到了异常。有什么我做错了吗?

    public void sharePicture(MenuItem shareItem) {
    MenuItemCompat.getActionProvider(shareItem);
    Drawable drawable = itemImage.getDrawable();

    Bitmap picture = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    picture.compress(Bitmap.CompressFormat.PNG, 100, stream);

    final Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/png");
    shareIntent.putExtra(Intent.EXTRA_STREAM, picture);
    startActivity(Intent.createChooser(shareIntent, "Share image using"));
}

1 个答案:

答案 0 :(得分:0)

引用the documentationEXTRA_STREAM是:

  

内容:包含与Intent关联的数据流的URI,与ACTION_SEND一起使用以提供正在发送的数据。

对于Bitmap EXTRA_STREAM请求,您未image/png加入ACTION_SEND。您将Uri指向PNG EXTRA_STREAM。理想情况下,您可以通过FileProvider执行此操作,但如果targetSdkVersion低于24,则暂时可以使用Uri.fromFile()