Example usage for android.os Environment getExternalStorageState

List of usage examples for android.os Environment getExternalStorageState

Introduction

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

Prototype

public static String getExternalStorageState() 

Source Link

Document

Returns the current state of the primary shared/external storage media.

Usage

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
 * on device's file system depending incoming parameters.
 *
 * @param context        Application context
 * @param preferExternal Whether prefer external location for cache
 * @return Cache {@link File directory}.<br />
 * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
 * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
 *//*from   w  ww  .j  av  a  2s  .c  o m*/
public static File getCacheDirectory(Context context, boolean preferExternal) {
    File appCacheDir = null;
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    } catch (IncompatibleClassChangeError e) { // (sh)it happens too (Issue #989)
        externalStorageState = "";
    }
    if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState) && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

public static boolean checkExternalStorageState() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {/*  w ww.jav  a2  s . co  m*/
        // Something else is wrong. It may be one of many other states, but
        // all we need
        // to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

    if (!mExternalStorageWriteable || !mExternalStorageAvailable) {
        // we cannot download the floor plan on the sdcard
        return false;
    }
    return true;
}

From source file:MainActivity.java

public boolean isExternalStorageWritable() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return true;
    }/*from  www .  jav  a 2 s  . co  m*/
    return false;
}

From source file:my.extensions.app.AudioFileLister.java

private boolean musicDirectoryReadable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state);
}

From source file:Main.java

/**
 * @return checks if the sdcard is mounted
 *///from  ww  w . ja  v a  2 s  .  co m
public static boolean isSDCardMounted() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

From source file:com.bukanir.android.utils.Utils.java

public static boolean isStorageAvailable() {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

From source file:MainActivity.java

public boolean isExternalStorageReadable() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
        return true;
    }/*from   www  .j  a v a  2 s .  c  o  m*/
    return false;
}

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
 * on device's file system depending incoming parameters.
 *
 * @param context        Application context
 * @param preferExternal Whether prefer external location for cache
 * @return Cache {@link File directory}.<br />
 * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
 * {@link android.content.Context#getCacheDir() Context.getCacheDir()} returns null).
 *//*from  w w w  .  ja  va 2s .co  m*/
public static File getCacheDirectory(Context context, boolean preferExternal, boolean cache, String customDir) {
    File appCacheDir = null;
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    } catch (IncompatibleClassChangeError e) { // (sh)it happens too (Issue #989)
        externalStorageState = "";
    }
    if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState) && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, cache, customDir);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath;
        cacheDirPath = "/data/data/" + context.getPackageName() + "/" + customDir + "/";

        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

public static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }// w  ww .jav  a2s  . c o m
    return false;
}

From source file:Main.java

public static boolean isSDCARDMounted() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWritable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWritable = false;
    } else {// w w  w .jav  a 2 s.c  o m
        mExternalStorageAvailable = mExternalStorageWritable = false;
    }
    return mExternalStorageAvailable && mExternalStorageWritable;
}