我正在尝试在我的应用中为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"));
}
答案 0 :(得分:0)
引用the documentation,EXTRA_STREAM
是:
内容:包含与Intent关联的数据流的URI,与ACTION_SEND一起使用以提供正在发送的数据。
对于Bitmap
EXTRA_STREAM
请求,您未image/png
加入ACTION_SEND
。您将Uri
指向PNG EXTRA_STREAM
。理想情况下,您可以通过FileProvider
执行此操作,但如果targetSdkVersion
低于24,则暂时可以使用Uri.fromFile()
。