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 getStorePath() {
    if (mStorePath == null) {
        String path = Environment.getExternalStorageDirectory().getPath();
        if (path == null || !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            path = mContext.getFilesDir().getPath();
        }//  ww  w.  j ava 2s. com
        if (!path.endsWith("/")) {
            path = path + "/";
        }
        mStorePath = path;
    }
    return mStorePath;
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;
    boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {//from w  w  w.j a va2 s .c om
        writeSucc = false;
    }

    File fileDir = new File((folderPath));
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return writeSucc;
}

From source file:Main.java

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

From source file:Main.java

public static boolean isExternalStorageAvailable(Context context) {
    if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
        return true;
    }//from w w w.jav a2s. c  o  m
    return false;
}

From source file:Main.java

public static boolean hasSDCard() { // SD????????
    String status = Environment.getExternalStorageState();
    return status.equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static boolean hasMainSDCard() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        //            boolean writable = checkFsWritable();
        //            return writable;
        return true;
    }//from  ww w. j  a va  2s . c  o  m
    return false;
}

From source file:Main.java

public static boolean isExternalStorageWriteable() {
    boolean writealbe = false;
    long start = System.currentTimeMillis();
    if (TextUtils.equals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState())) {
        File esd = Environment.getExternalStorageDirectory();
        if (esd.exists() && esd.canWrite()) {
            File file = new File(esd, ".696E5309-E4A7-27C0-A787-0B2CEBF1F1AB");
            if (file.exists()) {
                writealbe = true;//from   w  w w  .  j a va  2 s. co m
            } else {
                try {
                    writealbe = file.createNewFile();
                } catch (IOException e) {
                    Log.w(TAG, "isExternalStorageWriteable() can't create test file.");
                }
            }
        }
    }
    long end = System.currentTimeMillis();
    Log.i(TAG, "Utility.isExternalStorageWriteable(" + writealbe + ") cost " + (end - start) + "ms.");
    return writealbe;
}

From source file:Main.java

public static File getOwnCacheDirectory(Context context, String cacheDir) {
    File appCacheDir = null;/* w ww .  j a va  2 s  .  com*/
    if (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;
}

From source file:Main.java

/**
 * Checks if there is enough Space on SDCard
 *
 * @param needSize Size to Check/*from   w w w.j ava  2s . c om*/
 * @return True if the Update will fit on SDCard, false if not enough space on SDCard Will also return false, if the
 *         SDCard is not mounted as read/write
 */
public static boolean enoughSpaceOnSdCard(long needSize) {
    String status = Environment.getExternalStorageState();
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        return false;
    }
    return (needSize < getRealSizeOnSdcard());
}

From source file:Main.java

public static boolean storageReady() {
    String cardstatus = Environment.getExternalStorageState();
    if (cardstatus.equals(Environment.MEDIA_REMOVED) || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
            || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
            || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        return false;
    } else {/*from ww w  .  j ava 2 s.com*/
        return true;
    }
}