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

/**
 * Checks if external storage is available for read and write.
 * /*from www. j a v a 2  s .  c  om*/
 * @return True-If the external storage is writable.
 */
private static boolean isExternalStorageWritable() {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

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

    switch (state) {
    case Environment.MEDIA_MOUNTED:
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
        break;//from   ww  w  . ja  v a  2s  . co  m
    case Environment.MEDIA_MOUNTED_READ_ONLY:
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
        break;
    default:
        // 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;
        break;
    }
    return mExternalStorageAvailable && mExternalStorageWriteable;
}

From source file:it.feio.android.omninotes.utils.StorageManager.java

public static boolean checkStorage() {
    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 {//from  w w  w . ja va  2 s  . com
        // 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;
    }
    return mExternalStorageAvailable && mExternalStorageWriteable;
}

From source file:Main.java

public static boolean sdCardIsWork() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return true;
    }/*from   w  w  w. j  a  va  2  s .  c  o m*/
    return false;
}

From source file:Main.java

public static boolean checkSD() {
    String status = Environment.getExternalStorageState();
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {/*  w  ww  .j av  a2s. c  o m*/
        return false;
    }
}

From source file:com.docd.purefm.test.JavaFileTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    try {/*from w w w .j  a va2  s.c  o m*/
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignored
    }

    final String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        throw new RuntimeException(
                "Make sure the external storage is mounted read-write before running this test");
    }
    try {
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignored
    }
    assertTrue(testDir.mkdirs());

    // prepare a test file
    try {
        FileUtils.write(test1, "test");
    } catch (IOException e) {
        throw new RuntimeException("Failed to create test file: " + e);
    }
}

From source file:com.docd.purefm.test.PFMFileUtilsTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    final String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        throw new RuntimeException(
                "Make sure the external storage is mounted read-write before running this test");
    }//from   ww  w.  j a v a 2  s .c  om
    try {
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignored
    }
    assertTrue(testDir.mkdirs());
}

From source file:com.docd.purefm.test.MediaStoreUtilsTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    assertTrue(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));

    doTearDown();// w ww .j a  va2  s  . c o  m
    TEST_ROOT.mkdirs();
    assertTrue(TEST_ROOT.exists());
    assertTrue(TEST_ROOT.isDirectory());
}

From source file:com.docd.purefm.test.FileFactoryTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    try {//  www . j  av a 2 s. c  om
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignored
    }

    final String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        throw new RuntimeException(
                "Make sure the external storage is mounted read-write before running this test");
    }
    try {
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignore
    }
    assertTrue(testDir.mkdirs());
}

From source file:Main.java

/**
 * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
 * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
 *
 * @param context  Application context//from  ww w  .  j av a  2  s .c  o m
 * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
 * @return Cache {@link File directory}
 */
public static File getOwnCacheDirectory(Context context, String cacheDir, boolean preferExternal) {
    File appCacheDir = null;
    if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
    }
    if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}