Example usage for android.content Context getExternalMediaDirs

List of usage examples for android.content Context getExternalMediaDirs

Introduction

In this page you can find the example usage for android.content Context getExternalMediaDirs.

Prototype

public abstract File[] getExternalMediaDirs();

Source Link

Document

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place media files.

Usage

From source file:github.daneren2005.dsub.util.FileUtil.java

public static File getDefaultMusicDirectory(Context context) {
    if (DEFAULT_MUSIC_DIR == null) {
        File[] dirs;//from   ww  w  .j  a v  a  2s  .  c  o  m
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dirs = context.getExternalMediaDirs();
        } else {
            dirs = ContextCompat.getExternalFilesDirs(context, null);
        }

        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);

            // Some devices seem to have screwed up the new media directory API.  Go figure.  Try again with standard locations
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                dirs = ContextCompat.getExternalFilesDirs(context, null);

                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 {
                    Log.w(TAG, "Stupid OEM's messed up media dir API added in 5.0");
                }
            }
        }
    }

    return DEFAULT_MUSIC_DIR;
}