[Android] BitmapCache: don't return a recycled bitmap from the cache

Ludovic Fauvet git at videolan.org
Wed Oct 17 17:32:29 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Wed Oct 17 17:26:49 2012 +0200| [1455e989840d69cd614d69b93a6f668c3a5556f6] | committer: Ludovic Fauvet

BitmapCache: don't return a recycled bitmap from the cache

Android tends to recycle bitmaps automatically when a view is destroyed
regardless of whether it is still being used somewhere else. And that
can lead to a crash of this kind:
    RuntimeException: Canvas: trying to use a recycled bitmap

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=1455e989840d69cd614d69b93a6f668c3a5556f6
---

 vlc-android/src/org/videolan/vlc/BitmapCache.java |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/vlc-android/src/org/videolan/vlc/BitmapCache.java b/vlc-android/src/org/videolan/vlc/BitmapCache.java
index cc1fb60..005b625 100644
--- a/vlc-android/src/org/videolan/vlc/BitmapCache.java
+++ b/vlc-android/src/org/videolan/vlc/BitmapCache.java
@@ -69,6 +69,11 @@ public class BitmapCache {
         final Bitmap b = mMemCache.get(key);
         if (LOG_ENABLED)
             Log.d(TAG, (b == null) ? "Cache miss" : "Cache found");
+        if (b != null && b.isRecycled()) {
+            /* A recycled bitmap cannot be used again */
+            mMemCache.remove(key);
+            return null;
+        }
         return b;
     }
 
@@ -78,7 +83,7 @@ public class BitmapCache {
     }
 
     public Bitmap getBitmapFromMemCache(int resId) {
-        return mMemCache.get("res:" + resId);
+        return getBitmapFromMemCache("res:" + resId);
     }
 
     public void addBitmapToMemCache(int resId, Bitmap bitmap) {



More information about the Android mailing list