[Android] [PATCH 3/4] Generate thumbnails from dimension values

Geoffrey Métais geoffrey.metais at gmail.com
Thu Apr 2 16:54:46 CEST 2015


- No need to calculate from display density, frameworks gives use right values already
- Generate thumbs or downscale cover at the right size
- avoid useless Bitamp decode
---
 vlc-android/src/org/videolan/vlc/Thumbnailer.java     |  6 ++----
 vlc-android/src/org/videolan/vlc/util/BitmapUtil.java | 13 +++++++++----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/Thumbnailer.java b/vlc-android/src/org/videolan/vlc/Thumbnailer.java
index 22715fb..88d8a03 100644
--- a/vlc-android/src/org/videolan/vlc/Thumbnailer.java
+++ b/vlc-android/src/org/videolan/vlc/Thumbnailer.java
@@ -56,13 +56,11 @@ public class Thumbnailer implements Runnable {
     protected Thread mThread;
     private LibVLC mLibVlc;
     private int totalCount;
-    private final float mDensity;
     private final String mPrefix;
 
     public Thumbnailer(Context context, Display display) {
         DisplayMetrics metrics = new DisplayMetrics();
         display.getMetrics(metrics);
-        mDensity = metrics.density;
         mPrefix = context.getResources().getString(R.string.thumbnail);
     }
 
@@ -172,8 +170,8 @@ public class Thumbnailer implements Runnable {
             if (item.getArtworkURL() != null)
                 continue; //no need for thumbnail, we have a cover
 
-            int width = (int) (120 * mDensity);
-            int height = (int) (75 * mDensity);
+            int width = (VLCApplication.getAppResources().getDimensionPixelSize(R.dimen.grid_card_thumb_width));
+            int height = (VLCApplication.getAppResources().getDimensionPixelSize(R.dimen.grid_card_thumb_height));
 
             //Get bitmap
             byte[] b = mLibVlc.getThumbnail(item.getLocation(), width, height);
diff --git a/vlc-android/src/org/videolan/vlc/util/BitmapUtil.java b/vlc-android/src/org/videolan/vlc/util/BitmapUtil.java
index 377f3c7..c26f41b 100644
--- a/vlc-android/src/org/videolan/vlc/util/BitmapUtil.java
+++ b/vlc-android/src/org/videolan/vlc/util/BitmapUtil.java
@@ -22,9 +22,11 @@ package org.videolan.vlc.util;
 
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.MediaDatabase;
+import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
@@ -108,25 +110,28 @@ public class BitmapUtil {
     private static Bitmap readCoverBitmap(String path) {
         if (path == null)
             return null;
+        Resources res = VLCApplication.getAppResources();
         String uri = Uri.decode(path);
         if (uri.startsWith("file://"))
             uri = uri.substring(7);
         Bitmap cover = null;
         BitmapFactory.Options options = new BitmapFactory.Options();
+        int height = res.getDimensionPixelSize(R.dimen.grid_card_thumb_height);
+        int width = res.getDimensionPixelSize(R.dimen.grid_card_thumb_width);
 
         /* Get the resolution of the bitmap without allocating the memory */
         options.inJustDecodeBounds = true;
         BitmapFactory.decodeFile(uri, options);
 
         if (options.outWidth > 0 && options.outHeight > 0) {
+            if (options.outWidth > width){
+                options.outWidth = width;
+                options.outHeight = height;
+            }
             options.inJustDecodeBounds = false;
 
             // Decode the file (with memory allocation this time)
             cover = BitmapFactory.decodeFile(uri, options);
-
-            if (cover != null) {
-                cover = Bitmap.createScaledBitmap(cover, options.outWidth, options.outHeight, false);
-            }
         }
 
         return cover;
-- 
2.1.0



More information about the Android mailing list