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

/**
 * Check SD card/*from  w ww  . j  ava  2 s.  com*/
 * @return true if SD card is mounted
 * @author mikewu
 */
public static boolean isSDCardAvailable() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        return true;
    else
        return false;
}

From source file:Main.java

/** Returns true if external storage is readable. */
public static boolean isExtStorageReadable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

From source file:Main.java

public static Bitmap readImageFile(String fileName) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }//  w w  w.  j av  a  2s.com
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.exists()) {
        return null;
    }
    return BitmapFactory.decodeFile(fileName);
}

From source file:Main.java

public static void clearAllCache(Context context) {
    deleteDir(context.getCacheDir());// w w w  .j a va  2s  .  c  o m
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        deleteDir(context.getExternalCacheDir());
    }
}

From source file:Main.java

public static void cleanExternalCache(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        deleteFilesByDirectory(context.getExternalCacheDir());
    }//from ww  w.j ava2 s.c  om
}

From source file:Main.java

public static File getDiskDir(String uniqueName) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File storageDirectory = Environment.getExternalStorageDirectory();
        if (storageDirectory != null) {
            File file = new File(storageDirectory + File.separator + uniqueName);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdir();/*  w w w  . j  a  va2  s  .c  o  m*/
            }
            return file;
        }
    }
    return null;
}

From source file:Main.java

public static boolean hasStorage(boolean requireWriteAccess) {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            return writable;
        } else {//from  ww w .j  av a  2s . com
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

From source file:Main.java

/** Returns true if external storage is readable and writable. */
public static boolean isExtStorageWritable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
}

From source file:Main.java

public static String getDiskCacheDir(Context context) {

    String cachePath = null;/* w ww . j a v a2  s .  c  om*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && !Environment.isExternalStorageRemovable() && context.getExternalCacheDir() != null) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return cachePath;
}

From source file:Main.java

/**
 * Return if the state of the media is Mounted
 * //w  ww  . j a v  a  2 s .  com
 * @return boolean if a SD is mounted on the device
 */
public static boolean isMediaMounted() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}