Failed binder transaction when putting an bitmap dynamically in a widget

有人能告诉我 活页夹交易失败错误的原因吗? 我可以在 logcat 中看到这个错误消息。 我得到这个错误,而试图把一个位图动态在一个小部件..。

113219 次浏览

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

你可以通过缩小图片大小来解决这个问题:

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

选择 newHeight,使其足够小(屏幕上每个正方形大约100个) ,并将其用于你的小部件,这样你的问题就解决了:)

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.

请参阅此 链接

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

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

压紧!

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

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

请参阅我的回答 < a href = “ https://stackoverflow. com/questions/24285546/false-binder-transaction-while-pass-bitmap-from-one-activity-to-another/27333282”> 本文 线。

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

通过将大型元素从一个活动传输到另一个活动,您正在超出绑定器事务缓冲区。