Example usage for android.os Environment getStorageState

List of usage examples for android.os Environment getStorageState

Introduction

In this page you can find the example usage for android.os Environment getStorageState.

Prototype

@Deprecated
public static String getStorageState(File path) 

Source Link

Usage

From source file:at.jclehner.rxdroid.Backup.java

public static String getStorageState() {
    final String state;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        state = Environment.getStorageState(DIRECTORY);
    else/*w  w  w .j  a  v  a 2s .co m*/
        state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) && !DIRECTORY.canWrite()) {
        Log.d(TAG, "Storage state reported as MEDIA_MOUNTED, but " + DIRECTORY + " is not writeable");

        return Environment.MEDIA_MOUNTED_READ_ONLY;
    }

    return state;
}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableCacheRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalCacheDirs();
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }//from  ww  w .jav a 2s  . co  m

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalCacheDir();
    }

    // Worst case, resort to internal storage
    return context.getCacheDir();
}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableFilesRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalFilesDirs(null);
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }//  w w w . ja v a  2  s  . c om

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalFilesDir(null);
    }

    // Worst case, resort to internal storage
    return context.getFilesDir();
}