diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-04-24 17:24:09 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-04-24 17:24:09 -0700 |
commit | 8a332a20ec272d59fe74520825b18017a8f0cac3 (patch) | |
tree | 3ef90dca10ad5ac011d54df2df3f1fe60b7be2e8 /src/github | |
parent | 8da0f3b4c69a0d782fecd57707d7659178716cdd (diff) | |
download | dsub-8a332a20ec272d59fe74520825b18017a8f0cac3.tar.gz dsub-8a332a20ec272d59fe74520825b18017a8f0cac3.tar.bz2 dsub-8a332a20ec272d59fe74520825b18017a8f0cac3.zip |
For Lolipop use new API's to determine which external dir is the SD card
Diffstat (limited to 'src/github')
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index 2903960b..990eae06 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -42,6 +42,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; +import android.os.Environment; import android.support.v4.content.ContextCompat; import android.util.Log; import github.daneren2005.dsub.domain.Artist; @@ -433,12 +434,8 @@ public class FileUtil { dirs = ContextCompat.getExternalFilesDirs(context, null); } - for(int i = dirs.length - 1; i >= 0; i--) { - DEFAULT_MUSIC_DIR = new File(dirs[i], "music"); - if(dirs[i] != null) { - break; - } - } + DEFAULT_MUSIC_DIR = new File(getBestDir(dirs), "music"); + Log.d(TAG, "Default: " + DEFAULT_MUSIC_DIR.getAbsolutePath()); if (!DEFAULT_MUSIC_DIR.exists() && !DEFAULT_MUSIC_DIR.mkdirs()) { Log.e(TAG, "Failed to create default dir " + DEFAULT_MUSIC_DIR); @@ -447,13 +444,7 @@ public class FileUtil { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { dirs = ContextCompat.getExternalFilesDirs(context, null); - for(int i = dirs.length - 1; i >= 0; i--) { - DEFAULT_MUSIC_DIR = new File(dirs[i], "music"); - if(dirs[i] != null) { - break; - } - } - + DEFAULT_MUSIC_DIR = new File(getBestDir(dirs), "music"); if (!DEFAULT_MUSIC_DIR.exists() && !DEFAULT_MUSIC_DIR.mkdirs()) { Log.e(TAG, "Failed to create default dir " + DEFAULT_MUSIC_DIR); } else { @@ -465,6 +456,26 @@ public class FileUtil { return DEFAULT_MUSIC_DIR; } + private static File getBestDir(File[] dirs) { + // Past 5.0 we can query directly for SD Card + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + for(int i = 0; i < dirs.length; i++) { + if(dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) { + return dirs[i]; + } + } + } + + // Before 5.0, we have to guess. Most of the time the SD card is last + for(int i = dirs.length - 1; i >= 0; i--) { + if(dirs[i] != null) { + return dirs[i]; + } + } + + // Should be impossible to be reached + return dirs[0]; + } public static File getMusicDirectory(Context context) { String path = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, getDefaultMusicDirectory(context).getPath()); |