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 boolean isHaveSDCard() {
    String status = Environment.getExternalStorageState();
    return status.equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

/**
 * Checks if {@link Environment}.MEDIA_MOUNTED is returned by
 * {@code getExternalStorageState()} and therefore external storage is read-
 * and writeable./*from   w  w  w.  jav a2s . c o  m*/
 */
public static boolean isExtStorageAvailable() {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

From source file:Main.java

private static File getAlbumDir() {
    File storageDir = null;/*w  w w. j  a v a2 s  .c o m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "Telegram");
        if (!storageDir.mkdirs()) {
            if (!storageDir.exists()) {
                //                    FileLog.d("tmessages", "failed to create directory");
                return null;
            }
        }
    } else {
        //            FileLog.d("tmessages", "External storage is not mounted READ/WRITE.");
    }

    return storageDir;
}

From source file:Main.java

public static String getOutputMediaFolderPath() {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }/*from   w  w w. j a va2  s .c  o  m*/

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "HDMIRxDemo");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }
    // return path string of the folder
    return mediaStorageDir.getAbsolutePath();
}

From source file:Main.java

private static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
}

From source file:Main.java

public static boolean isEnoughMemory(int fileSize) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        int freeSpace = getFreeMemory(Environment.getExternalStorageDirectory().getAbsolutePath());
        if (freeSpace > fileSize) {
            return true;
        } else//from  w w  w  .  ja  va 2s. c  om
            return false;

    } else
        return false;
}

From source file:Main.java

/**
 * Returns true if the android external storage is writeable.
 *//*  w w  w  . j  av  a  2s .  co  m*/
public static boolean isExternalStorageWriteable() {
    return (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));
}

From source file:Main.java

public static String assetToSd(Context ctx, String inFileName, String outFileName) {

    try {/* w  ww .  j  a  v  a2 s  .c o m*/
        InputStream is = ctx.getAssets().open(inFileName);
        if (is == null) {
            return null;
        }

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = sd + "/uexTencent/";
            File filePath = new File(path);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String fileName = path + outFileName;
            FileOutputStream fos = new FileOutputStream(fileName);

            byte[] buf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = is.read(buf)) != -1) {
                fos.write(buf, 0, len);
                total++;
            }
            is.close();
            fos.close();
            fileSize = total;
            return fileName;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * @param context//from w ww  . ja  va2s .c  o m
 * @param dirName Only the folder name, not contain full path.
 * @return app_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            ? context.getExternalCacheDir().getPath()
            : context.getCacheDir().getPath();

    return cachePath + File.separator + dirName;
}

From source file:Main.java

public static List<String> getExtSDCardPaths() {
    List<String> paths = new ArrayList<String>();
    String extFileStatus = Environment.getExternalStorageState();
    File extFile = Environment.getExternalStorageDirectory();
    if (extFileStatus.endsWith(Environment.MEDIA_UNMOUNTED) && extFile.exists() && extFile.isDirectory()
            && extFile.canWrite()) {
        paths.add(extFile.getAbsolutePath());
    }//from   www  . ja  v a 2  s  . c om
    try {
        // obtain executed result of command line code of 'mount', to judge
        // whether tfCard exists by the result
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("mount");
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        int mountPathIndex = 1;
        while ((line = br.readLine()) != null) {
            // format of sdcard file system: vfat/fuse
            if ((!line.contains("fat") && !line.contains("fuse") && !line.contains("storage"))
                    || line.contains("secure") || line.contains("asec") || line.contains("firmware")
                    || line.contains("shell") || line.contains("obb") || line.contains("legacy")
                    || line.contains("data")) {
                continue;
            }
            String[] parts = line.split(" ");
            int length = parts.length;
            if (mountPathIndex >= length) {
                continue;
            }
            String mountPath = parts[mountPathIndex];
            if (!mountPath.contains("/") || mountPath.contains("data") || mountPath.contains("Data")) {
                continue;
            }
            File mountRoot = new File(mountPath);
            if (!mountRoot.exists() || !mountRoot.isDirectory() || !mountRoot.canWrite()) {
                continue;
            }
            boolean equalsToPrimarySD = mountPath.equals(extFile.getAbsolutePath());
            if (equalsToPrimarySD) {
                continue;
            }
            paths.add(mountPath);
        }
    } catch (IOException e) {
        Log.e(TAG, "IOException:" + e.getMessage());
    }
    return paths;
}