Example usage for android.os Environment MEDIA_UNMOUNTED

List of usage examples for android.os Environment MEDIA_UNMOUNTED

Introduction

In this page you can find the example usage for android.os Environment MEDIA_UNMOUNTED.

Prototype

String MEDIA_UNMOUNTED

To view the source code for android.os Environment MEDIA_UNMOUNTED.

Click Source Link

Document

Storage state if the media is present but not mounted.

Usage

From source file:Main.java

public static boolean isExternalStorageMounted() {

    boolean canRead = Environment.getExternalStorageDirectory().canRead();
    boolean onlyRead = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    boolean unMounted = Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED);

    return !(!canRead || onlyRead || unMounted);
}

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   w w  w.  j  ava 2  s  .  c o m
        return true;
    }
}

From source file:Main.java

private static boolean isStorageReady() {
    String cardstatus = Environment.getExternalStorageState();
    if (cardstatus.equals(Environment.MEDIA_REMOVED) || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
            || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
            || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        return false;
    } else {/*  w  ww  .java  2 s.c  o m*/
        if (cardstatus.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
}

From source file:Main.java

public static void verifyExternalStorageAvailability() {
    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)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        RuntimeException e = new RuntimeException(
                "ODK reports :: SDCard error: " + Environment.getExternalStorageState());
        throw e;//w  ww.  j  ava  2s  . c o m
    }
}

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());
    }/* w  w  w .java  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;
}

From source file:Main.java

/**
 * Returns the current state of the storage device that provides the given path.
 * @param state "getExternalStorageState()"
 *///from w  w w  .  ja va 2 s  .c  o  m
public static String getExternalStorageState(String state) {
    if (TextUtils.isEmpty(state)) {
        return UNKNOWN;
    }

    switch (state) {
    case Environment.MEDIA_BAD_REMOVAL://bad_removal
        return "MEDIA_BAD_REMOVAL";
    case Environment.MEDIA_CHECKING://checking
        return "MEDIA_CHECKING";
    case Environment.MEDIA_EJECTING://ejecting
        return "MEDIA_EJECTING";
    case Environment.MEDIA_MOUNTED://mounted
        return "MEDIA_MOUNTED";
    case Environment.MEDIA_MOUNTED_READ_ONLY://mounted_read_only
        return "MEDIA_MOUNTED_READ_ONLY";
    case Environment.MEDIA_NOFS://nofs
        return "MEDIA_NOFS";
    case Environment.MEDIA_REMOVED://removed
        return "MEDIA_REMOVED";
    case Environment.MEDIA_SHARED://shared
        return "MEDIA_SHARED";
    case Environment.MEDIA_UNKNOWN://unknown
        return "MEDIA_UNKNOWN";
    case Environment.MEDIA_UNMOUNTABLE://unmountable
        return "MEDIA_UNMOUNTABLE";
    case Environment.MEDIA_UNMOUNTED://unmounted
        return "MEDIA_UNMOUNTED";
    default:
        return UNKNOWN;
    }
}

From source file:org.digitalcampus.oppia.utils.FileUtils.java

public static boolean createDirs(Context ctx) {
    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)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        Log.d(TAG, "card status: " + cardstatus);
        return false;
    }//  w ww . j  a  va2  s. c  o  m

    String[] dirs = { FileUtils.getCoursesPath(ctx), FileUtils.getMediaPath(ctx),
            FileUtils.getDownloadPath(ctx) };

    for (String dirName : dirs) {
        File dir = new File(dirName);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.d(TAG, "can't mkdirs");
                return false;
            }
        } else {
            if (!dir.isDirectory()) {
                Log.d(TAG, "not a directory");
                return false;
            }
        }
    }

    return true;
}

From source file:com.mpower.mintel.android.application.MIntel.java

/**
 * Creates required directories on the SDCard (or other external storage)
 * //from  w  w  w.j  ava  2  s  .c om
 * @throws RuntimeException
 *             if there is no SDCard or the directory exists as a non
 *             directory
 */
public static void createMIntelDirs() throws RuntimeException {
    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)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        RuntimeException e = new RuntimeException(
                "mIntel reports :: SDCard error: " + Environment.getExternalStorageState());
        throw e;
    }

    String[] dirs = { MINTEL_ROOT, FORMS_PATH, INSTANCES_PATH, CACHE_PATH, METADATA_PATH, PRESCRIPTION_PATH };

    for (String dirName : dirs) {
        File dir = new File(dirName);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                RuntimeException e = new RuntimeException(
                        "mIntel reports :: Cannot create directory: " + dirName);
                throw e;
            }
        } else {
            if (!dir.isDirectory()) {
                RuntimeException e = new RuntimeException(
                        "mIntel reports :: " + dirName + " exists, but is not a directory");
                throw e;
            }
        }
    }

    String[] fileNames = { "pres_n.ogg", "call_n.ogg" };
    copyAudioFiles(fileNames);
}

From source file:org.digitalcampus.oppia.utils.storage.ExternalStorageStrategy.java

public boolean isStorageAvailable(Context ctx) {
    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)
            || cardStatus.equals(Environment.MEDIA_SHARED)) {
        Log.d(TAG, "card status: " + cardStatus);
        return false;
    } else {//  ww w .j a  va 2  s .c  om
        return true;
    }
}

From source file:com.radicaldynamic.groupinform.application.Collect.java

/**
 * Creates required directories on the SDCard (or other external storage)
 * @throws RuntimeException if there is no SDCard or the directory exists as a non directory
 *//* w  ww . j  a v a2 s .  co  m*/
public static void createODKDirs() throws RuntimeException {
    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)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        RuntimeException e = new RuntimeException(
                "ODK reports :: SDCard error: " + Environment.getExternalStorageState());
        throw e;
    }

    String[] dirs = { ODK_ROOT, FORMS_PATH, INSTANCES_PATH, CACHE_PATH, METADATA_PATH };

    for (String dirName : dirs) {
        File dir = new File(dirName);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                RuntimeException e = new RuntimeException("ODK reports :: Cannot create directory: " + dirName);
                throw e;
            }
        } else {
            if (!dir.isDirectory()) {
                RuntimeException e = new RuntimeException(
                        "ODK reports :: " + dirName + " exists, but is not a directory");
                throw e;
            }
        }
    }
}