From da21f68c103f6377b3371ec84ac4b0f7f01ec562 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Tue, 17 Jun 2014 21:08:08 +0200 Subject: [PATCH] load album art on our own --- .../blinkenlights/android/vanilla/Song.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/Song.java b/src/ch/blinkenlights/android/vanilla/Song.java index eb575b2d..3017f750 100644 --- a/src/ch/blinkenlights/android/vanilla/Song.java +++ b/src/ch/blinkenlights/android/vanilla/Song.java @@ -31,6 +31,8 @@ import android.net.Uri; import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import android.util.LruCache; +import java.io.File; +import java.io.FileInputStream; import java.io.FileDescriptor; import android.util.Log; @@ -125,6 +127,9 @@ public class Song implements Comparable { private static class CoverCache extends LruCache { private final Context mContext; + // Possible coverart names if we are going to load the cover on our own + private static String[] coverNames = { "cover.jpg", "cover.png", "album.jpg", "album.png", "artwork.jpg", "artwork.png", "art.jpg", "art.png" }; + public CoverCache(Context context) { super(6 * 1024 * 1024); @@ -138,10 +143,25 @@ public class Song implements Comparable { ContentResolver res = mContext.getContentResolver(); Log.v("VanillaMusic", "Cache miss on key "+key); try { + FileDescriptor fileDescriptor = null; ParcelFileDescriptor parcelFileDescriptor = res.openFileDescriptor(uri, "r"); if (parcelFileDescriptor != null) { - FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); + fileDescriptor = parcelFileDescriptor.getFileDescriptor(); + } else { + String basePath = (new File(key.path)).getParentFile().getAbsolutePath(); // ../ of the currently playing file + for (String coverFile: coverNames) { + File guessedFile = new File( basePath + "/" + coverFile); + if (guessedFile.exists() && !guessedFile.isDirectory()) { + Log.v("VanillaMusic", "Found album artwork at "+guessedFile.getAbsolutePath()); + FileInputStream fis = new FileInputStream(guessedFile); + fileDescriptor = fis.getFD(); + break; + } + } + } + + if (fileDescriptor != null) { BitmapFactory.Options bopts = new BitmapFactory.Options(); bopts.inPreferredConfig = Bitmap.Config.RGB_565; bopts.inJustDecodeBounds = true; @@ -165,7 +185,7 @@ Log.v("VanillaMusic", "Cache miss on key "+key); */ private static int getSampleSize(FileDescriptor fd, BitmapFactory.Options bopts) { int sampleSize = 1; /* default sample size */ - long maxVal = 400*400; /* max number of pixels we are accepting */ + long maxVal = 600*600; /* max number of pixels we are accepting */ BitmapFactory.decodeFileDescriptor(fd, null, bopts); long hasPixels = bopts.outHeight * bopts.outWidth;