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.
This commit is contained in:
Martin Disch 2018-06-20 14:16:41 +02:00
commit 0400bda7cd
No known key found for this signature in database
GPG key ID: DB6C1FF7E550D904
2 changed files with 22 additions and 0 deletions

View file

@ -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<InputStream> {
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);