具有位图的小部件上的 RuntimeException (Android)

发布于 2024-09-15 08:05:15 字数 4134 浏览 5 评论 0原文

我的应用程序有带有 ImageView 和 TextView 的小部件。在 WidgetProvider 的 onUpdate() 方法上,我以这种方式将位图放入 ImageView 中:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_btn);
Bitmap bitmap2 = BitmapManager.setColor(bitmap, red, green, blue);
views.setImageViewBitmap(R.id.image, bitmap2);

setColor() 方法是这样的:

public synchronized static Bitmap setColor(Bitmap org, float r, float g, float b)
{
    sColorMatrix.setScale(r, g, b, 1);
    sColorPaint.setColorFilter(new ColorMatrixColorFilter(sColorMatrix));
    // RGB_565 is faster, but loses transparency
    Bitmap ret = Bitmap.createBitmap(org.getWidth(), org.getHeight(), Bitmap.Config.ARGB_8888);
    try{
        sColorCanvas.setBitmap(ret);
        //sColorCanvas.drawColor(Color.);
        sColorCanvas.drawBitmap(org, 0, 0, sColorPaint);
    } catch (Throwable t){

    }

    return ret;
}

问题是有时小部件会抛出 RuntimeException 因为有人回收了位图,而我不知道该怎么办。一些建议?

如果有用的话我可以附上堆栈跟踪。谢谢你!


这是堆栈跟踪:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@462605a8
   at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
   at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
   at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
   at android.widget.ImageView.onDraw(ImageView.java:923)
   at android.view.View.draw(View.java:6739)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365)
   at android.widget.ListView.dispatchDraw(ListView.java:3046)
   at android.view.View.draw(View.java:6845)
   at android.widget.AbsListView.draw(AbsListView.java:2257)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
   at android.view.ViewRoot.draw(ViewRoot.java:1422)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:144)
   at android.app.ActivityThread.main(ActivityThread.java:4937)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)

my app has widgets that have an ImageView and a TextView. On the onUpdate() method of the WidgetProvider, I put a Bitmap inside the ImageView this way:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_btn);
Bitmap bitmap2 = BitmapManager.setColor(bitmap, red, green, blue);
views.setImageViewBitmap(R.id.image, bitmap2);

setColor() method is this:

public synchronized static Bitmap setColor(Bitmap org, float r, float g, float b)
{
    sColorMatrix.setScale(r, g, b, 1);
    sColorPaint.setColorFilter(new ColorMatrixColorFilter(sColorMatrix));
    // RGB_565 is faster, but loses transparency
    Bitmap ret = Bitmap.createBitmap(org.getWidth(), org.getHeight(), Bitmap.Config.ARGB_8888);
    try{
        sColorCanvas.setBitmap(ret);
        //sColorCanvas.drawColor(Color.);
        sColorCanvas.drawBitmap(org, 0, 0, sColorPaint);
    } catch (Throwable t){

    }

    return ret;
}

The problem is that sometimes the widget throws a RuntimeException because somebody has recycled the Bitmap, and I don't know what to do. Some suggestions?

I can attach the stacktrace if it can be useful. Thank you!


This is the stacktrace:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@462605a8
   at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
   at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
   at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
   at android.widget.ImageView.onDraw(ImageView.java:923)
   at android.view.View.draw(View.java:6739)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365)
   at android.widget.ListView.dispatchDraw(ListView.java:3046)
   at android.view.View.draw(View.java:6845)
   at android.widget.AbsListView.draw(AbsListView.java:2257)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
   at android.view.View.draw(View.java:6742)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
   at android.view.ViewRoot.draw(ViewRoot.java:1422)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:144)
   at android.app.ActivityThread.main(ActivityThread.java:4937)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)

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

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

发布评论

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

评论(2

も让我眼熟你 2024-09-22 08:05:15

解决方案是从代码中删除对 Bitmap#recycle 的所有调用。垃圾收集器将处理未引用的位图。

The solution is to remove all calls to Bitmap#recycle from your code. The garbage collector will take care of unreferenced bitmaps.

方觉久 2024-09-22 08:05:15

看起来 bitmap2 超出了 GC 范围,然后当绘制时它就消失了。

使 bitmap2 成为静态字段。这应该会阻止它被收集,直到包含的类被销毁。

It looks like bitmap2 goes out of scope get GC'd then when it is time be drawn it has disappeared.

Make bitmap2 a static field. This should stop it being collected until the containing class is destroyed.

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