[Android] Synchronize Http images cache operations

Geoffrey Métais git at videolan.org
Mon Oct 2 10:25:27 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Oct  2 10:11:27 2017 +0200| [695f7b1f1c3cfe14280233971cbba8b16177d36a] | committer: Geoffrey Métais

Synchronize Http images cache operations

> https://code.videolan.org/videolan/vlc-android/commit/695f7b1f1c3cfe14280233971cbba8b16177d36a
---

 .../src/org/videolan/vlc/util/HttpImageLoader.java | 24 ++++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/HttpImageLoader.java b/vlc-android/src/org/videolan/vlc/util/HttpImageLoader.java
index ad371d8ab..0b991677c 100644
--- a/vlc-android/src/org/videolan/vlc/util/HttpImageLoader.java
+++ b/vlc-android/src/org/videolan/vlc/util/HttpImageLoader.java
@@ -95,12 +95,14 @@ public class HttpImageLoader implements Callbacks {
 
     @Nullable
     public static Bitmap getBitmapFromIconCache(String imageUrl) {
-        if (iconsMap.containsKey(imageUrl)) {
-            Bitmap bd = iconsMap.get(imageUrl).get();
-            if (bd != null) {
-                return bd;
-            } else
-                iconsMap.remove(imageUrl);
+        synchronized (iconsMap) {
+            if (iconsMap.containsKey(imageUrl)) {
+                Bitmap bd = iconsMap.get(imageUrl).get();
+                if (bd != null) {
+                    return bd;
+                } else
+                    iconsMap.remove(imageUrl);
+            }
         }
         return null;
     }
@@ -108,17 +110,21 @@ public class HttpImageLoader implements Callbacks {
     @Nullable
     public static Bitmap downloadBitmap(String imageUrl) {
         HttpURLConnection urlConnection = null;
+        InputStream in = null;
         Bitmap icon = getBitmapFromIconCache(imageUrl);
         if (icon != null)
             return icon;
         try {
-            URL url = new URL(imageUrl);
+            final URL url = new URL(imageUrl);
             urlConnection = (HttpURLConnection) url.openConnection();
-            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
+            in = new BufferedInputStream(urlConnection.getInputStream());
             icon = BitmapFactory.decodeStream(in);
-            iconsMap.put(imageUrl, new SoftReference<>(icon));
+            synchronized (iconsMap) {
+                iconsMap.put(imageUrl, new SoftReference<>(icon));
+            }
         } catch (IOException|IllegalArgumentException ignored) {
         } finally {
+            Util.close(in);
             if (urlConnection != null)
                 urlConnection.disconnect();
         }



More information about the Android mailing list