From 5f07f932f39cf36da2c46a62c7eff30dc2cc9a6c Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Fri, 29 Aug 2014 20:55:03 +0200 Subject: [PATCH 1/2] set HARD_CACHE_CAPACITY to 0 added try catch for OOM in ImageFetcher --- src/android/Library/src/ImageFetcher.java | 52 ++++++++++++----------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/android/Library/src/ImageFetcher.java b/src/android/Library/src/ImageFetcher.java index f2adc9a..f69c918 100644 --- a/src/android/Library/src/ImageFetcher.java +++ b/src/android/Library/src/ImageFetcher.java @@ -178,29 +178,33 @@ public class ImageFetcher { */ @Override protected Bitmap doInBackground(Integer... params) { - position = params[0]; - if (isCancelled()) { - return null; - } - Bitmap thumb = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), position, 12345, - MediaStore.Images.Thumbnails.MINI_KIND, null); - if (isCancelled()) { - return null; - } - if (thumb == null) { - return null; - } else { - if (isCancelled()) { - return null; - } else { - if (rotate != 0) { - Matrix matrix = new Matrix(); - matrix.setRotate(rotate); - thumb = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(), thumb.getHeight(), matrix, true); - } - return thumb; - } - } + try { + position = params[0]; + if (isCancelled()) { + return null; + } + Bitmap thumb = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), position, 12345, + MediaStore.Images.Thumbnails.MINI_KIND, null); + if (isCancelled()) { + return null; + } + if (thumb == null) { + return null; + } else { + if (isCancelled()) { + return null; + } else { + if (rotate != 0) { + Matrix matrix = new Matrix(); + matrix.setRotate(rotate); + thumb = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(), thumb.getHeight(), matrix, true); + } + return thumb; + } + } + }catch(OutOfMemoryError error) { + return null; + } } @@ -278,7 +282,7 @@ public class ImageFetcher { * Garbage Collector. */ - private static final int HARD_CACHE_CAPACITY = 100; + private static final int HARD_CACHE_CAPACITY = 0; private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds // Hard cache, with a fixed maximum capacity and a life duration From fae8c7d0aafd2d0bfab21684228fd8079f72476d Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Tue, 2 Sep 2014 06:18:08 +0200 Subject: [PATCH 2/2] automatic clear the cache on OutOfMemoryError --- src/android/Library/src/ImageFetcher.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/android/Library/src/ImageFetcher.java b/src/android/Library/src/ImageFetcher.java index f69c918..88255bd 100644 --- a/src/android/Library/src/ImageFetcher.java +++ b/src/android/Library/src/ImageFetcher.java @@ -203,6 +203,7 @@ public class ImageFetcher { } } }catch(OutOfMemoryError error) { + clearCache(); return null; } @@ -282,7 +283,7 @@ public class ImageFetcher { * Garbage Collector. */ - private static final int HARD_CACHE_CAPACITY = 0; + private static final int HARD_CACHE_CAPACITY = 100; private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds // Hard cache, with a fixed maximum capacity and a life duration @@ -366,8 +367,8 @@ public class ImageFetcher { * after a certain inactivity delay. */ public void clearCache() { - // sHardBitmapCache.clear(); - // sSoftBitmapCache.clear(); + sHardBitmapCache.clear(); + sSoftBitmapCache.clear(); } /**