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:org.jinzora.util.DrawableManager.java

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
    String state = Environment.getExternalStorageState();
    if (!externalStorageAvailable()) {
        if (DBG)//from  w ww . j a  v  a  2 s .  c o  m
            Log.d(TAG, "External storage not available; fetching from network");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }

    File thumbFile = getLocalFile(urlString);
    if (thumbFile.exists()) {
        return new FileInputStream(thumbFile);
    }

    if (!cacheToDisk || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        if (DBG)
            Log.d(TAG, "Fetching from network and not writing to disk");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    } else {
        if (DBG)
            Log.d(TAG, "Fetching from network and storing on disk");
        File thumbDir = thumbFile.getParentFile();
        if (!thumbDir.exists()) {
            thumbDir.mkdirs();
        }
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        InputStream is = response.getEntity().getContent();

        OutputStream out = new FileOutputStream(thumbFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        return new FileInputStream(thumbFile);
    }
}

From source file:com.zia.freshdocs.widget.adapter.CMISAdapter.java

/**
 * Retrieve the children from the specified node.
 * //from  w  w w  .  ja  va 2s. co m
 * @param position
 *            n-th child position
 */
public void getChildren(int position) {
    final NodeRef ref = getItem(position);

    // For folders display the contents for the specified URI
    if (ref.isFolder()) {
        mStack.push(mCurrentState);
        String uuid = ref.getContent();
        mCurrentState = new Pair<String, NodeRef[]>(uuid, null);
        getChildren(uuid);
    }
    // For non-folders try a download (if there is an external storage card)
    else {
        String storageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(storageState)) {
            downloadContent(ref, new Handler() {
                public void handleMessage(Message msg) {
                    boolean done = msg.getData().getBoolean("done");
                    if (done) {
                        dismissProgressDlg();

                        File file = (File) mDlThread.getResult();
                        if (file != null) {
                            viewContent(file, ref);
                        }
                    } else {
                        int value = msg.getData().getInt("progress");
                        if (value > 0) {
                            mProgressDlg.setProgress(value);
                        }
                    }
                }
            });
        }
    }
}

From source file:com.brainasylum.andruid.UniqueIdentifierManager.java

private static boolean isSdCardMounted() {
    return "mounted".equals(Environment.getExternalStorageState());
}

From source file:com.att.android.arodatacollector.utils.AROCollectorUtils.java

/**
 * Returns a value indicating whether or not the device SD card is mounted.
 * //  www .  j ava 2  s . c  o  m
 * @return A boolean value that is "true" if the SD card is mounted, and
 *         "false" if it is not.
 */
public boolean checkSDCardMounted() {
    final String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_REMOVED) || !state.equals(Environment.MEDIA_MOUNTED)
            || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        return true;
    } else {
        return false;
    }
}

From source file:com.brainasylum.andruid.UniqueIdentifierManager.java

private static boolean isSdCardMountedReadOnly() {
    return "mounted_ro".equals(Environment.getExternalStorageState());
}

From source file:cmu.troy.applogger.AppService.java

private boolean checkExternalStorageAvailable() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {/*w w w.  j  a  va  2 s.c  o  m*/
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    if (!(mExternalStorageAvailable && mExternalStorageWriteable)) {
        return false;
    } else
        return true;
}

From source file:org.opendatakit.utilities.ODKFileUtils.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)) {
        throw new RuntimeException("ODK reports :: SDCard error: " + Environment.getExternalStorageState());
    }//from w  ww . jav  a 2s .  c o  m
}

From source file:com.logilite.vision.camera.CameraLauncher.java

private String getTempDirectoryPath() {
    File cache = null;//from   w ww  .  j a v a  2s .  c  om

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + cordova.getActivity().getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = cordova.getActivity().getCacheDir();
    }

    // Create the cache directory if it doesn't exist
    cache.mkdirs();
    return cache.getAbsolutePath();
}

From source file:model.Document.java

/**
 * @return the validity in memory of this Document. A document is valid in
 *         memory during a week// w  w w .  j ava 2s .c o m
 */
public boolean isOnMemory() {
    if (mLoadedDate.plusWeeks(1).isAfterNow()) {
        // Exits the function if the storage is not writable!
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.d("ClaroClient", "Missing SDCard");
            return false;
        }

        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        File file = new File(root.getAbsolutePath(), getTitle() + "." + getExtension());
        return file.exists() && file.canRead();
    }
    return false;
}

From source file:com.twapime.app.util.AsyncImageLoader.java

/**
 * // w w w . jav a 2  s .c om
 */
private void loadCacheDir() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File sdDir = Environment.getExternalStorageDirectory();
        cacheDir = new File(sdDir.getAbsolutePath() + "/TwAPIme/cache");
        //
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
    } else {
        cacheDir = context.getCacheDir();
    }
}