lucasr / smoothie

[DEPRECATED] Easy async loading for Android's ListView/GridView
985 stars 163 forks source link

RuntimeException: Canvas: trying to use a recycled bitmap #8

Open Flitskikker opened 11 years ago

Flitskikker commented 11 years ago

Hi,

I'm trying to use your wonderful Smoothie library in a new project, based on the bitmapcache example. It works in the emulator, but on my device (S2 @ Stock 4.0.4), it crashes when I scroll the ListView.

Maybe it has something to do with the bitmapcache fade in thingy?

Thanks in advance for checking out.

Cheers, Martijn

02-09 02:57:41.600 E/AndroidRuntime(18033): FATAL EXCEPTION: main 02-09 02:57:41.600 E/AndroidRuntime(18033): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@415aaf40 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1038) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.Canvas.drawBitmap(Canvas.java:1142) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:394) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:198) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ImageView.onDraw(ImageView.java:910) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11184) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ListView.drawChild(ListView.java:3231) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.AbsListView.dispatchDraw(AbsListView.java:2277) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ListView.dispatchDraw(ListView.java:3226) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.AbsListView.draw(AbsListView.java:3760) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.FrameLayout.draw(FrameLayout.java:450) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.FrameLayout.draw(FrameLayout.java:450) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2291) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2210) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.os.Handler.dispatchMessage(Handler.java:99) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.os.Looper.loop(Looper.java:137) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.app.ActivityThread.main(ActivityThread.java:4511) 02-09 02:57:41.600 E/AndroidRuntime(18033): at java.lang.reflect.Method.invokeNative(Native Method) 02-09 02:57:41.600 E/AndroidRuntime(18033): at java.lang.reflect.Method.invoke(Method.java:511) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 02-09 02:57:41.600 E/AndroidRuntime(18033): at dalvik.system.NativeStart.main(Native Method)

Flitskikker commented 11 years ago

Tested some more. In the ListLoader, displayItem(); if I replace the transition code:

BitmapDrawable emptyDrawable = new BitmapDrawable(itemView.getResources());

TransitionDrawable fadeInDrawable = new TransitionDrawable(new Drawable[] { emptyDrawable, result });

holder.image.setImageDrawable(fadeInDrawable);
fadeInDrawable.startTransition(200);

by simply:

holder.image.setImageDrawable(result);

...the application doesn't crash on my phone.

However, I tried your sample (including the fade ins) and it doesn't crash. Did I do something wrong?

atla commented 11 years ago

So what happened here? I still get the same error as Flitskikker and can only fix it with the same workaround. How can i not run into this while still fading in the image?

atla commented 11 years ago

Oh - just a quick hint... i updated to the latest BitmapCache version from git that also includes setRecyclePolicy on the builder (i was curious to try that out) and the crash seems to be gone. It internally also uses a newer version of the DiskLRUCache.

Flitskikker commented 11 years ago

I updated the BitmapCache library from Git already a while ago, and it would still crash on my device. It works fine on the emulator (4.2), though. Since I've updated my device to JB 4.2.2, it now also works on there.

I've just updated the library again and it still crashes when scrolling on an emulator running 2.2. Can't test it on my device now unfortunately, but I suppose it would still crash (on 4.0.4). Same workaround: disable the fade ins and it works.

@atla: A few questions:

I'm using the BitmapCache from GIT and the DiskLRUCache 1.3.1 jar (not the GIT source as library).

Thanks in advance!

atla commented 11 years ago

Hi,

sorry for taking so long to respond. Actually the issue still occured to me. It didnt when i answered this post so i thought its fixed. But it is still occuring randomly. The only "fix" that i have - what i currently activated - is the Recycle policy RecyclePolicy.DISABLED ... which i guess has a huge impact on memory and basically makes the caching useless.

Still not sure where exactly this bug comes from.. sorry.

regards, marcus

jansouza commented 11 years ago

Hi,

I fixed this problem, first switch ImageView by CacheableImageView. The BitmapCache need CacheableImageView to recycle correctly.

So, ViewHolder should be: class ViewHolder { public CacheableImageView image; public TextView title; }

And don't forget on layout: <uk.co.senab.bitmapcache.CacheableImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" />

And second, you need remove TransitionDrawable (see Flitskikker comment above), again the BitmapCache need CacheableImageView to recycle correctly.

I hope this helps.

Thanks.

jerrellmardis commented 11 years ago

I'm getting this error randomly as well. @jansouza How does the TransitionDrawable have anything to do with the way recycling happens?

* UPDATE 4/25 * I've removed the TransitionDrawable code and this still seems to happen. It seems to happen less but I've noticed it happen a few times on my testing device (Hauwei Comet).

blaztriglav commented 11 years ago

Had the same issue, Flitskikker's solution of removing the transition worked for me.