【问题标题】:Share Option for PNGsPNG的共享选项
【发布时间】:2017-07-05 22:24:08
【问题描述】:

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

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

【问题讨论】:

  • 我添加了代码来压缩我的位图 ...不,您不这样做,因为您不能...您将位图保存/压缩到您从未使用过的流中再次使用它......位图将始终占用width * heigth * bytes_per_pixel内存

标签: java android bitmap png share


【解决方案1】:

引用the documentation,EXTRA_STREAM是:

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

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

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2022-11-24
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多