[Android] Fix a possible OutOfMemoryError when loading the default thumbnail

Ludovic Fauvet git at videolan.org
Sat Oct 13 20:14:49 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Sat Oct 13 20:06:26 2012 +0200| [bbaa3c64b55c1f3b3cc920d10a46b1100171ef62] | committer: Ludovic Fauvet

Fix a possible OutOfMemoryError when loading the default thumbnail

Creating a bitmap from resources costs memory and CPU, do it only once
for the default thumbnail.

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

 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java    |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 0c5d7fa..58780f3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -48,6 +48,7 @@ public class VideoListAdapter extends ArrayAdapter<Media>
     private int mSortDirection = 1;
     private int mSortBy = SORT_BY_TITLE;
     private boolean mListMode = false;
+    private Bitmap mDefaultThumbnail;
 
     public VideoListAdapter(Context context) {
         super(context, 0);
@@ -147,15 +148,17 @@ public class VideoListAdapter extends ArrayAdapter<Media>
         Media media = getItem(position);
         holder.title.setText(media.getTitle());
 
-        Bitmap thumbnail;
         if (media.getPicture() != null) {
             //FIXME Warning: the thumbnails are upscaled in the grid view!
-            thumbnail = media.getPicture();
+            final Bitmap thumbnail = media.getPicture();
             holder.thumbnail.setImageBitmap(thumbnail);
         } else {
             // set default thumbnail
-            thumbnail = BitmapFactory.decodeResource(v.getResources(), R.drawable.thumbnail);
-            holder.thumbnail.setImageBitmap(thumbnail);
+            if (mDefaultThumbnail == null) {
+                mDefaultThumbnail = BitmapFactory.decodeResource(
+                        v.getResources(), R.drawable.thumbnail);
+            }
+            holder.thumbnail.setImageBitmap(mDefaultThumbnail);
         }
 
         ColorStateList titleColor = v.getResources().getColorStateList(R.color.list_title);



More information about the Android mailing list