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

public static String getSDPath() {
    File sdDir = null;/*from  w  ww  .  j av a  2s.c  o m*/
    boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
    if (sdCardExist) {
        sdDir = Environment.getExternalStorageDirectory();
    } else {
        sdDir = Environment.getRootDirectory();
    }

    return sdDir.toString();
}

From source file:Main.java

public static String getFileRoot(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File external = context.getExternalFilesDir(null);
        if (external != null) {
            return external.getAbsolutePath();
        }//from  www  .  j  ava 2s.co  m
    }
    return context.getFilesDir().getAbsolutePath();
}

From source file:Main.java

public static String getCacheDir(Context ctx) {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable() ? ctx.getExternalCacheDir().getPath()
                    : ctx.getCacheDir().getPath();
}

From source file:Main.java

private static String getFileRoot(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File external = context.getExternalFilesDir(null);
        if (external != null) {
            return external.getAbsolutePath();
        }/*from   w ww.  ja  v a 2 s.c om*/
    }

    return context.getFilesDir().getAbsolutePath();
}

From source file:Main.java

public static boolean hasSDCard() {
    return android.os.Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

From source file:Main.java

private static boolean checkExternalMedia() {
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // Can read and write the media
        mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageWriteable = false;
    } else {/*from   ww w  . ja va  2s .  c o  m*/
        mExternalStorageWriteable = false;
    }
    return mExternalStorageWriteable;
}

From source file:Main.java

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

From source file:Main.java

public static long getFreeDiskSpace() {
    String status = Environment.getExternalStorageState();
    long freeSpace = 0;
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        try {/*  www .  j a v  a  2s  .  com*/
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            freeSpace = availableBlocks * blockSize / 1024;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        return -1;
    }
    return (freeSpace);
}

From source file:Main.java

public static void setCacheRoot(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        mCacheRoot = Environment.getExternalStorageDirectory().getPath() + "/";
    } else {//from w w  w . j av a  2s  .  c  o  m
        mCacheRoot = context.getCacheDir().getPath() + "/";
    }
}

From source file:Main.java

/**
 * Check is SD card is ready for read/* w ww  .j a v a  2s.co  m*/
 * 
 * @return
 */
public static boolean isSDReadable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}