From 023f0ef37fdb0aba232b27665792c96927266a02 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Mon, 26 Jun 2017 19:38:06 +0200 Subject: [PATCH] Workaround for samsung devices. Looks like Samsunb broke getExternalMediaDirs() on 5.x as it tends to return a null File sometimes. This code checks for it and falls back to the old case on total failure. --- .../android/medialibrary/MediaLibrary.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java index b67c583b..f760aba8 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java @@ -154,14 +154,22 @@ public class MediaLibrary { private static ArrayList discoverDefaultMediaPaths(Context context) { ArrayList defaultPaths = new ArrayList<>(); + // Try to discover media paths using getExternalMediaDirs() on 5.x and newer if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { for (File file : context.getExternalMediaDirs()) { + // Seems to happen on some Samsung 5.x devices. :-( + if (file == null) + continue; + String path = file.getAbsolutePath(); int match = path.indexOf("/Android/media/"); // From Environment.DIR_ANDROID + Environment.DIR_MEDIA (both hidden) if (match >= 0) defaultPaths.add(path.substring(0, match)); } - } else { + } + + // Fall back to old API and some guessing if nothing was found (yet). + if (defaultPaths.size() == 0) { // this should always exist defaultPaths.add(Environment.getExternalStorageDirectory().getAbsolutePath()); // this *may* exist