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:ie.programmer.catcher.browser.FileBrowserEngine.FileBrowserEngine.java

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

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted. Else - Android defines cache directory on
 * device's file system.// w w w. j ava 2  s .  c o m
 * 
 * @param context Application context
 * @return Cache {@link File directory}
 */
public static File getCacheDirectory(Context context) {
    File appCacheDir = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:com.zeusky.star.star.java

public static void createSharePicDir() {
    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    if (!sdCardExist) {
        Toast.makeText(m_instance, "???", Toast.LENGTH_SHORT).show();
    } else {/*from www . ja va 2  s  .  c o  m*/
        String dir = Environment.getExternalStorageDirectory() + File.separator + "zeusky.popstar";
        Log.d("shareSDK", "I am need this dir " + dir);
        File snapShotDir = new File(dir);
        if (!snapShotDir.exists()) {
            snapShotDir.mkdir();
        }
        methodsRun.injectOtherDir(dir + File.separator);
    }
}

From source file:net.sileht.lullaby.Utils.java

public static void checkStorage() {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {//w  w w  .j  av  a  2  s .  c o  m
        // Something else is wrong. It may be one of many other states, but
        // all we need
        // to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
}

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

/**
 * Creates required directories on the SDCard (or other external storage)
 * //  www.  j a  v a 2s  . c o m
 * @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:fr.inria.ucn.collectors.LlamaCollector.java

@Override
public void run(Context c, long ts) {
    try {// w  w  w  . j  a v  a2  s  .  co  m
        String state = Environment.getExternalStorageState();
        File llamaAreas = new File(Environment.getExternalStorageDirectory() + "/Llama/Llama_Areas.txt");

        if (Environment.MEDIA_MOUNTED.equals(state) && llamaAreas.exists()) {
            JSONObject data = new JSONObject();
            data.put("source_file", llamaAreas.toString());

            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(llamaAreas), 1024);
                String line;
                JSONObject a = new JSONObject();
                while ((line = in.readLine()) != null) {
                    String[] tmp = line.trim().split("\\|");
                    JSONArray wifi = new JSONArray();
                    JSONArray cell = new JSONArray();
                    for (int i = 1; i < tmp.length; i++) {
                        String[] tmp2 = tmp[i].split("\\:");
                        if (tmp2[0].trim().equalsIgnoreCase("W")) {
                            wifi.put(tmp2[1].trim());
                        } else {
                            // FIXME: figure out what these values are ?!?
                            JSONObject cid = new JSONObject();
                            cid.put("1", Integer.parseInt(tmp2[0].trim()));
                            cid.put("2", Integer.parseInt(tmp2[1].trim()));
                            cid.put("3", Integer.parseInt(tmp2[2].trim()));
                            cell.put(cid);
                        }
                    }
                    JSONObject o = new JSONObject();
                    o.put("wifi_networks", wifi); // wifi networks associated to this area
                    o.put("cells", cell); // cell ids associated to this area
                    a.put(tmp[0].trim(), o);
                }
                data.put("locations", a);

                // done
                Helpers.sendResultObj(c, "llama_location", ts, data);

            } catch (FileNotFoundException e) {
                Log.w(Constants.LOGTAG, "Llama exports not available", e);
            } catch (IOException e) {
                Log.w(Constants.LOGTAG, "Llama exports not available", e);
            }

            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                }
        } else {
            Log.w(Constants.LOGTAG, "Llama exports not available, file=" + llamaAreas.toString() + ", exists="
                    + llamaAreas.exists());
        }

    } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object", jex);
    }
}

From source file:com.kg.emailalbum.mobile.util.CacheManager.java

/**
 * Get a cache directory for a specific task.
 * //  w w  w .  j a v a2  s  .com
 * @param subdir
 *            The name of the task, will result as a subdirectory of the
 *            cache root.
 * @return The cache directory for this task.
 */
public File getCacheDir(String subdir) {
    if (subdir == null) {
        subdir = "";
    }

    // Default root is the application context cache.
    File result = new File(mContext.getCacheDir(), subdir);

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        // If an external storage is available, use it as it will prevent
        // from overloading the internal memory.
        result = new File(Environment.getExternalStorageDirectory(), "data/EmailAlbum/.cache/" + subdir);
    }

    // Physically create the directory (and its parents) if it does not
    // exist.
    if (!result.exists()) {
        result.mkdirs();
    }

    File noMedia = new File(result, ".nomedia");
    try {
        noMedia.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(LOG_TAG, "Error : ", e);
    }

    // Log.i(LOG_TAG, "Using dir " + result + " for cache");
    return result;
}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

/**
 * Returns snapshot at index modulo the length of the list, if index is < 0, it returns a random snapshot
 *///  w w w.  ja  va2 s. c o m
private static List<WifiReading> getSnapshot(int index, Context c) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can at least read the external storage
        File root = c.getExternalCacheDir();
        File[] snapshotFiles = root.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                if (filename.endsWith(FILE_EXTENSION)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        if (snapshotFiles.length > 0) {
            if (index < 0) {
                Random rand = new Random();
                index = rand.nextInt(snapshotFiles.length);
            } else {
                index = index % snapshotFiles.length;
            }
            try {
                // read file into a string
                FileInputStream fstream = new FileInputStream(snapshotFiles[index]);
                Log.d(TAG, snapshotFiles[index].getName());
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String fileContents = "";
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    fileContents += strLine;
                }

                // make a json array out of the string
                JSONArray json = new JSONArray(fileContents);

                // parse the json array
                return jsonToReadings(json);
            } catch (Exception e) {
                Log.e(TAG, "Could not read file.");
                return null;
            }
        } else {
            // there are no cached snapshots
            return null;
        }
    } else {
        // we cannot read the external storage
        return null;
    }
}

From source file:com.docd.purefm.test.CommandLineFileTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    final String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        throw new RuntimeException(
                "Make sure the external storage is mounted read-write before running this test");
    }//from w  ww .j  a va2s  .c  o m
    try {
        FileUtils.forceDelete(testDir);
    } catch (IOException e) {
        //ignored
    }
    assertTrue(testDir.mkdirs());

    // prepare a test file
    try {
        FileUtils.write(test1, "test");
    } catch (IOException e) {
        throw new RuntimeException("Failed to create test file: " + e);
    }
}

From source file:Main.java

public static File getSDDir(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // We can read and write the media
        return context.getExternalFilesDir(null);
    } else {/*from ww w  .j a v  a2 s .  c o  m*/
        // Load another directory, probably local memory
        return context.getFilesDir();
    }
}