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:com.sinpo.xnfc.nfc.Util.java

public static boolean initDownPath(String path) {
    if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();/*from  www  .  ja  v  a  2 s  . c o  m*/
            return true;
        }
    }
    return false;
}

From source file:com.android.fastlibrary.AppException.java

/**
 * ?/*from ww w .j a v a2  s . c  om*/
 *
 * @param excp
 */
public void saveErrorLog(Exception excp) {
    String errorlog = "errorlog.txt";
    String savePath = "";
    String logFilePath = "";
    FileWriter fw = null;
    PrintWriter pw = null;
    try {
        //?SD?
        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OSChina/Log/";
            File file = new File(savePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            logFilePath = savePath + errorlog;
        }
        //SD?
        if (logFilePath == "") {
            return;
        }
        File logFile = new File(logFilePath);
        if (!logFile.exists()) {
            logFile.createNewFile();
        }
        fw = new FileWriter(logFile, true);
        pw = new PrintWriter(fw);
        pw.println("--------------------" + (new Date().toLocaleString()) + "---------------------");
        excp.printStackTrace(pw);
        pw.close();
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:net.peterkuterna.android.apps.devoxxfrsched.util.ImageDownloader.java

Bitmap downloadBitmap(String url) {
    final int IO_BUFFER_SIZE = 4 * 1024;

    File cacheFile = null;//from  ww w  .ja  v  a  2s.c om
    try {
        MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
        mDigest.update(url.getBytes());
        final String cacheKey = bytesToHexString(mDigest.digest());
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                    + File.separator + "data" + File.separator + mContext.getPackageName() + File.separator
                    + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp");
        }
    } catch (NoSuchAlgorithmException e) {
        // Oh well, SHA-1 not available (weird), don't cache bitmaps.
    }

    HttpGet getRequest = null;
    try {
        getRequest = new HttpGet(url);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "Error while constructing get request: " + e.getMessage());
        return null;
    }

    try {
        HttpResponse response = mClient.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            final byte[] respBytes = EntityUtils.toByteArray(entity);

            if (cacheFile != null) {
                try {
                    cacheFile.getParentFile().mkdirs();
                    cacheFile.createNewFile();
                    FileOutputStream fos = new FileOutputStream(cacheFile);
                    fos.write(respBytes);
                    fos.close();
                } catch (FileNotFoundException e) {
                    Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e);
                } catch (IOException e) {
                    Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e);
                }
            }

            return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length);
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(TAG, "Error while retrieving bitmap from " + url, e);
    }
    return null;
}

From source file:com.android.volley.misc.Utils.java

@SuppressLint("NewApi")
private static boolean isExternalMounted() {
    if (Utils.hasGingerbread()) {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable();
    }//ww w. j ava2  s  . c o  m
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

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

/**
 * Retrieve the children from the specified node.  
 * @param position n-th child position//from   w w  w  .j a va2  s .c o  m
 */
public void getChildren(int position) {
    final NodeRef ref = getItem(position);

    // For folders display the contents for the specified URI
    if (ref.isFolder()) {
        _stack.push(_currentState);
        String uuid = ref.getContent();
        _currentState = 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) _dlThread.getResult();
                        if (file != null) {
                            viewContent(file, ref);
                        }
                    } else {
                        int value = msg.getData().getInt("progress");
                        if (value > 0) {
                            _progressDlg.setProgress(value);
                        }
                    }
                }
            });
        }
    }
}

From source file:com.blork.anpod.util.BitmapUtils.java

private static void manageCache(String slug, Context context) {
    String filename = slug + ".jpg";
    File folder;//from www  .  j  a  v  a 2  s  .  co m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                + File.separator + "data" + File.separator + "com.blork.anpod" + File.separator + "cache");
    } else {
        folder = context.getCacheDir();
    }

    int cacheSize = 15;
    Log.w("", "current file: " + filename);
    Log.w("", "Managing cache");
    File[] files = folder.listFiles();
    if (files == null || files.length <= cacheSize) {
        Log.w("", "Cache size is fine");
        return;
    }

    int count = files.length;

    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

    for (File f : files) {
        if (count > cacheSize && !filename.equals(f.getName())) {
            Log.w("", "Deleting " + f.getName());
            f.delete();
            count--;
        }
    }
}

From source file:com.ibm.watson.developer_cloud.android.text_to_speech.v1.TTSUtility.java

/**
 * Get storage path/*w ww  . j a v  a  2s  .c  o  m*/
 * @return
 */
private String getBaseDir() {
    String baseDir;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    } else {
        baseDir = "/data/data/" + getApplicationContext().getPackageName() + "/";
    }

    return baseDir;
}

From source file:com.mobicage.rogerthat.util.CachedDownloader.java

private CachedDownloader(MainService mainService) {
    this.mMainService = mainService;
    if (IOUtils.shouldCheckExternalStorageAvailable()) {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            this.mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            this.mExternalStorageWriteable = false;
        } else {/*from  w  ww .  ja v  a2 s.c  o m*/
            this.mExternalStorageWriteable = false;
        }
    } else {
        this.mExternalStorageWriteable = true;
    }
}

From source file:com.bourke.kitchentimer.utils.Utils.java

public static boolean isSdPresent() {
    return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}