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

发布于 2024-09-15 01:35:19 字数 89 浏览 4 评论 0原文

谁能告诉我绑定程序事务失败错误的原因吗?我可以在 logcat 中看到此错误消息。 我在尝试将位图动态放入小部件时收到此错误...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜心 2024-09-22 01:35:19

这是因为对 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)并将其用于您的小部件,您的问题将得到解决:)

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.

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

烟织青萝梦 2024-09-22 01:35:19

您可以将位图压缩为字节数组,然后在另一个活动中将其解压缩,如下所示。

压缩!!

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

解压!!

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

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);
月隐月明月朦胧 2024-09-22 01:35:19

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

请参考此链接

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.

refer this link

花辞树 2024-09-22 01:35:19

请参阅我的答案
线。

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

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

See my answer in this
thread.

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.

雨落星ぅ辰 2024-09-22 01:35:19

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

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

时光病人 2024-09-22 01:35:19

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文