[Android] ThumbnailsProvider with access to Android thumbs

Geoffrey Métais git at videolan.org
Tue Jun 13 15:00:56 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jun 13 10:50:56 2017 +0200| [40dbd5fefd0712f98163540fa17289331ab18e05] | committer: Geoffrey Métais

ThumbnailsProvider with access to Android thumbs

> https://code.videolan.org/videolan/vlc-android/commit/40dbd5fefd0712f98163540fa17289331ab18e05
---

 .../videolan/vlc/gui/helpers/AsyncImageLoader.java |  4 +-
 ...{ImageComposer.java => ThumbnailsProvider.java} | 68 +++++++++++++++-------
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/AsyncImageLoader.java b/vlc-android/src/org/videolan/vlc/gui/helpers/AsyncImageLoader.java
index cbdc9a4b7..4c2f817c0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/AsyncImageLoader.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/AsyncImageLoader.java
@@ -45,7 +45,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.media.MediaGroup;
 import org.videolan.vlc.util.HttpImageLoader;
-import org.videolan.vlc.util.ImageComposer;
+import org.videolan.vlc.util.ThumbnailsProvider;
 
 public class AsyncImageLoader {
 
@@ -137,7 +137,7 @@ public class AsyncImageLoader {
                 return null;
             String artworkUrl = item.getArtworkMrl();
             if (item instanceof MediaGroup)
-                return ImageComposer.getComposedImage((MediaGroup) item);
+                return ThumbnailsProvider.getComposedImage((MediaGroup) item);
             if (!TextUtils.isEmpty(artworkUrl) && artworkUrl.startsWith("http"))
                 return HttpImageLoader.downloadBitmap(artworkUrl);
             return AudioUtil.readCoverBitmap(Uri.decode(item.getArtworkMrl()), width);
diff --git a/vlc-android/src/org/videolan/vlc/util/ImageComposer.java b/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.java
similarity index 72%
rename from vlc-android/src/org/videolan/vlc/util/ImageComposer.java
rename to vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.java
index cd032c3f4..88c86b1be 100644
--- a/vlc-android/src/org/videolan/vlc/util/ImageComposer.java
+++ b/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.java
@@ -1,28 +1,12 @@
-/*****************************************************************************
- * ImageComposer.java
- *****************************************************************************
- * Copyright © 2017 VLC authors, VideoLAN and VideoLabs
- * Author: Geoffrey Métais
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
 package org.videolan.vlc.util;
 
+
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
+import android.media.ThumbnailUtils;
 import android.net.Uri;
+import android.provider.MediaStore;
+import android.support.annotation.WorkerThread;
 
 import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.R;
@@ -31,13 +15,37 @@ import org.videolan.vlc.gui.helpers.AudioUtil;
 import org.videolan.vlc.gui.helpers.BitmapCache;
 import org.videolan.vlc.media.MediaGroup;
 
-public class ImageComposer {
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+
+import static org.videolan.medialibrary.Medialibrary.THUMBS_FOLDER_NAME;
+
+public class ThumbnailsProvider {
 
-    private static final String TAG = "VLC/ImageComposer";
+    private static final String TAG = "VLC/ThumbnailsProvider";
 
     private static final int sImageWidth = VLCApplication.getAppResources().getDimensionPixelSize(VLCApplication.showTvUi() ? R.dimen.tv_grid_card_thumb_width : R.dimen.grid_card_thumb_width);
     private static final int MAX_IMAGES = 4;
 
+    @WorkerThread
+    public static Bitmap getThumbnail(final String filePath) {
+        final Bitmap bitmap= ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
+        if (bitmap != null) {
+            final File appDir = VLCApplication.getAppContext().getExternalFilesDir(null);
+            if (appDir != null && appDir.exists())
+                VLCApplication.runBackground(new Runnable() {
+                    @Override
+                    public void run() {
+                        saveOnDisk(bitmap, appDir.getAbsolutePath()+ THUMBS_FOLDER_NAME
+                                +"/"+ FileUtils.getFileNameFromPath(filePath)+".jpg");
+                    }
+                });
+        }
+        return bitmap;
+    }
+
+    @WorkerThread
     public static Bitmap getComposedImage(MediaGroup group) {
         BitmapCache bmc = BitmapCache.getInstance();
         String key = "group:"+group.getTitle();
@@ -133,4 +141,20 @@ public class ImageComposer {
         );
         return dstBmp;
     }
+
+    private static void saveOnDisk(Bitmap bitmap, String destPath) {
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
+        byte[] byteArray = stream.toByteArray();
+        FileOutputStream fos = null;
+        try {
+            fos = new FileOutputStream(destPath);
+            fos.write(byteArray);
+        } catch (java.io.IOException e) {
+            e.printStackTrace();
+        } finally {
+            Util.close(fos);
+            Util.close(stream);
+        }
+    }
 }



More information about the Android mailing list