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 String showFileAvailable() {
    String result = "";
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
        long blockSize = sf.getBlockSize();
        long blockCount = sf.getBlockCount();
        long availCount = sf.getAvailableBlocks();
        return showFileSize(availCount * blockSize) + " / " + showFileSize(blockSize * blockCount);
    }//from   w w  w.  ja v  a2s. c  o m
    return result;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/*w  w w  .ja  va  2s. c om*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageEmulated()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

public static String imageToLocal(Bitmap bitmap, String name) {
    String path = null;/*from   ww w.ja  v a2 s .c o m*/
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

From source file:Main.java

public static void saveWeiboBitmap(Bitmap bitmap, String filename) {
    String status = Environment.getExternalStorageState();
    if (bitmap == null || !status.equals(Environment.MEDIA_MOUNTED)) {
        return;/*from  w w  w.  java2  s  . c o m*/
    }
    File f = new File(filename);
    FileOutputStream fOut = null;
    boolean error = false;
    try {
        f.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        error = true;
    }

    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        error = true;
    }

    try {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    } catch (Exception e) {
        e.printStackTrace();
        error = true;
    }

    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;//from   w  w  w  .  j  a  va2 s  .  c  o m
    PrintStream out = null;
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return -1;
    }
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdir();
    }
    try {
        out = new PrintStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        ret = -2;
    } finally {
        out.flush();
        out.close();
        if (!bitmap.isRecycled())
            bitmap.recycle();
    }

    return ret;
}

From source file:Main.java

public static boolean isExistExternalStore() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {//  w  ww  .  j  a v a2 s . c  om
        return false;
    }
}

From source file:Main.java

/**
 * Helper Method to Test if external Storage is Available from
 * http://www.ibm.com/developerworks/xml/library/x-androidstorage/index.html
 *//*from   w  w  w  .ja v a 2  s  .com*/
public static boolean isExternalStorageAvailable() {
    boolean state = false;
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        state = true;
    }
    return state;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable() ? Environment.getExternalStorageDirectory().getPath()
                    : context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {/* w  ww . j av a 2  s . c  om*/
        writeSucc = false;
    }

    File fileDir = new File(folderPath);
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return writeSucc;
}

From source file:Main.java

private static boolean isHasSdcard() {
    String status = Environment.getExternalStorageState();
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {/*from   w  ww . j a  v  a  2  s.c  o  m*/
        return false;
    }
}