From 67257655a3a1921fa0837f5de34e9f5d3babc938 Mon Sep 17 00:00:00 2001 From: Adam Koch Date: Thu, 7 Nov 2013 17:07:04 -0500 Subject: Bitmapfun Sample: Minor updates/fixes. Change-Id: I6ac19a95a65ab5210f62d1f50d76f2b3e1b533ef --- .../training/displaying-bitmaps/manage-memory.jd | 45 ++++++++++++---------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'docs/html') diff --git a/docs/html/training/displaying-bitmaps/manage-memory.jd b/docs/html/training/displaying-bitmaps/manage-memory.jd index 0e1279e..7f2b4c5 100644 --- a/docs/html/training/displaying-bitmaps/manage-memory.jd +++ b/docs/html/training/displaying-bitmaps/manage-memory.jd @@ -160,13 +160,14 @@ a soft reference to the bitmap is placed in a {@link java.util.HashSet}, for possible reuse later with {@link android.graphics.BitmapFactory.Options#inBitmap}: -
HashSet<SoftReference<Bitmap>> mReusableBitmaps;
+
Set<SoftReference<Bitmap>> mReusableBitmaps;
 private LruCache<String, BitmapDrawable> mMemoryCache;
 
-// If you're running on Honeycomb or newer, create
-// a HashSet of references to reusable bitmaps.
+// If you're running on Honeycomb or newer, create a
+// synchronized HashSet of references to reusable bitmaps.
 if (Utils.hasHoneycomb()) {
-    mReusableBitmaps = new HashSet<SoftReference<Bitmap>>();
+    mReusableBitmaps =
+            Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
 }
 
 mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) {
@@ -243,25 +244,27 @@ protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
         Bitmap bitmap = null;
 
     if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
-        final Iterator<SoftReference<Bitmap>> iterator
-                = mReusableBitmaps.iterator();
-        Bitmap item;
-
-        while (iterator.hasNext()) {
-            item = iterator.next().get();
-
-            if (null != item && item.isMutable()) {
-                // Check to see it the item can be used for inBitmap.
-                if (canUseForInBitmap(item, options)) {
-                    bitmap = item;
-
-                    // Remove from reusable set so it can't be used again.
+        synchronized (mReusableBitmaps) {
+            final Iterator<SoftReference<Bitmap>> iterator
+                    = mReusableBitmaps.iterator();
+            Bitmap item;
+
+            while (iterator.hasNext()) {
+                item = iterator.next().get();
+
+                if (null != item && item.isMutable()) {
+                    // Check to see it the item can be used for inBitmap.
+                    if (canUseForInBitmap(item, options)) {
+                        bitmap = item;
+
+                        // Remove from reusable set so it can't be used again.
+                        iterator.remove();
+                        break;
+                    }
+                } else {
+                    // Remove from the set if the reference has been cleared.
                     iterator.remove();
-                    break;
                 }
-            } else {
-                // Remove from the set if the reference has been cleared.
-                iterator.remove();
             }
         }
     }
-- 
cgit v1.1