Android 将位图动态放入小部件时,活页夹事务失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3528735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:37:12  来源:igfitidea点击:

Failed binder transaction when putting an bitmap dynamically in a widget

androidbitmapwidget

提问by Eby

Can anybody tell me the reason for failed binder transactionerror? I can see this error message in logcat. I am getting this error while trying to put an bitmap dynamically in a widget...

谁能告诉我活页夹交易失败的原因?我可以在 logcat 中看到此错误消息。尝试在小部件中动态放置位图时出现此错误...

回答by GalDude33

This is caused because all the changes to the RemoteViews are serialised (e.g. setInt and setImageViewBitmap ). The bitmaps are also serialised into an internal bundle. Unfortunately this bundle has a very small size limit.

这是因为对 RemoteViews 的所有更改都是序列化的(例如 setInt 和 setImageViewBitmap )。位图也被序列化为一个内部包。不幸的是,这个捆绑包的大小限制非常小。

You can solve it by scaling down the image size this way:

您可以通过以下方式缩小图像大小来解决它:

 public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {

 final float densityMultiplier = context.getResources().getDisplayMetrics().density;        

 int h= (int) (newHeight*densityMultiplier);
 int w= (int) (h * photo.getWidth()/((double) photo.getHeight()));

 photo=Bitmap.createScaledBitmap(photo, w, h, true);

 return photo;
 }

Choose newHeight to be small enough (~100 for every square it should take on the screen) and use it for your widget, and your problem will be solved :)

选择 newHeight 足够小(屏幕上的每个正方形约 100)并将其用于您的小部件,您的问题将得到解决:)

回答by Nicolás Loaiza

You can compress the bitmap as an byte's array and then uncompress it in another activity, like this.

您可以将位图压缩为字节数组,然后在另一个活动中解压缩它,就像这样。

Compress!!

压缩!!

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bytes = stream.toByteArray(); 
        setresult.putExtra("BMP",bytes);

Uncompress!!

解压!!

        byte[] bytes = data.getByteArrayExtra("BMP");
        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

回答by dharam

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.

Binder 事务缓冲区有一个有限的固定大小,当前为 1Mb,由进程的所有正在进行的事务共享。因此,当有许多事务正在进行时,即使大多数单个事务的大小适中,也会抛出此异常。

refer this link

参考这个链接

回答by Balaji Dubey

See my answer in thisthread.

请参阅我在线程中的回答。

intent.putExtra("Some string",very_large_obj_for_binder_buffer);

intent.putExtra("Some string",very_large_obj_for_binder_buffer);

You are exceeding the binder transaction buffer by transferring large element(s) from one activity to another activity.

通过将大元素从一个活动转移到另一个活动,您超出了活页夹事务缓冲区。

回答by MartinC

I have solved this issue by storing images on internal storage and then using .setImageURI() rather than .setBitmap().

我通过将图像存储在内部存储上然后使用 .setImageURI() 而不是 .setBitmap() 解决了这个问题。

回答by Alexander Woodblock

The right approach is to use setImageViewUri()(slower) or the setImageViewBitmap()and recreating RemoteViews every time you update the notification.

正确的方法是在每次更新通知时使用setImageViewUri()(较慢)或setImageViewBitmap()重新创建RemoteViews。