From 0400bda7cd65c811e1e7932779c846e6668585da Mon Sep 17 00:00:00 2001 From: Martin Disch Date: Wed, 20 Jun 2018 14:16:41 +0200 Subject: [PATCH] Fix loading of large embedded album art Android's MediaMetadataRetriever.getEmbeddedPicture() fails on large images, because ID3 metadata that is considered too big is skipped. --- app/build.gradle | 1 + .../audiocover/AudioFileCoverFetcher.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 665d52d4..1ee84c29 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -137,5 +137,6 @@ dependencies { transitive = true } implementation 'com.google.code.gson:gson:2.8.2' + implementation 'com.mpatric:mp3agic:0.9.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } diff --git a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java index a675a869..930548a2 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java +++ b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java @@ -4,6 +4,10 @@ import android.media.MediaMetadataRetriever; import com.bumptech.glide.Priority; import com.bumptech.glide.load.data.DataFetcher; +import com.mpatric.mp3agic.ID3v2; +import com.mpatric.mp3agic.InvalidDataException; +import com.mpatric.mp3agic.Mp3File; +import com.mpatric.mp3agic.UnsupportedTagException; import java.io.ByteArrayInputStream; import java.io.File; @@ -48,6 +52,23 @@ public class AudioFileCoverFetcher implements DataFetcher { private static final String[] FALLBACKS = {"cover.jpg", "album.jpg", "folder.jpg", "cover.png", "album.png", "folder.png"}; private InputStream fallback(String path) throws FileNotFoundException { + // Method 1: use embedded high resolution album art if there is any + try { + Mp3File mp3File = new Mp3File(path); + if (mp3File.hasId3v2Tag()) { + ID3v2 id3v2Tag = mp3File.getId3v2Tag(); + byte[] imageData = id3v2Tag.getAlbumImage(); + if (imageData != null) { + return new ByteArrayInputStream(imageData); + } + } + // If there are any exceptions, we ignore them and continue to the other fallback method + } catch (IOException ignored) { + } catch (InvalidDataException ignored) { + } catch (UnsupportedTagException ignored) { + } + + // Method 2: look for album art in external files File parent = new File(path).getParentFile(); for (String fallback : FALLBACKS) { File cover = new File(parent, fallback);