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 boolean saveToSDCard(Bitmap bitmap) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return false;
    }/*from w  w  w. j  ava  2  s .  c o  m*/
    FileOutputStream fileOutputStream = null;
    File file = new File("/sdcard/myName/Download/");
    if (!file.exists()) {
        file.mkdirs();
    }
    String fileName = UUID.randomUUID().toString() + ".jpg";
    String filePath = "/sdcard/myName/Download/" + fileName;
    File f = new File(filePath);
    if (!f.exists()) {
        try {
            f.createNewFile();
            fileOutputStream = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } catch (IOException e) {
            return false;
        } finally {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

private static String getStorageDirectory() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? sdRootPath + FOLDER_NAME
            : appRootPath + FOLDER_NAME;
}

From source file:Main.java

public static boolean storeBitmapToFile(Bitmap bitmap, String filePath) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {/*from   w w w  .  j a  va2s  .  c o m*/
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bitmap.compress(CompressFormat.PNG, 50, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    return false;
}

From source file:Main.java

/**
 * Get the external storage path of the device
 *
 * @return The external storage path of the device.
 *//*ww  w .  jav a2  s.c o m*/
public static String getStorageRootDirectory() {
    try {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
    } catch (Exception e) {
        // Catch exception is trying to fix a crash inside of Environment.getExternalStorageState().
        e.printStackTrace();
        return null;
    }
    String rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    File file = new File(rootDir);
    if (!file.exists()) {
        if (!file.mkdirs()) {
            return null;
        }
    }
    return rootDir;
}

From source file:Main.java

public static void downloadImage(String imageUrl) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Log.d("TAG", "monted sdcard");
    } else {//  w  w  w  .  j  a  v  a  2s  .  c  o m
        Log.d("TAG", "has no sdcard");
    }
    HttpURLConnection con = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    File imageFile = null;
    try {
        URL url = new URL(imageUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setReadTimeout(15 * 1000);
        con.setDoInput(true);
        con.setDoOutput(true);
        bis = new BufferedInputStream(con.getInputStream());
        imageFile = new File(getImagePath(imageUrl));
        fos = new FileOutputStream(imageFile);
        bos = new BufferedOutputStream(fos);
        byte[] b = new byte[1024];
        int length;
        while ((length = bis.read(b)) != -1) {
            bos.write(b, 0, length);
            bos.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (con != null) {
                con.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (imageFile != null) {
    }
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/*  w w  w . java  2 s  .com*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            cachePath = context.getCacheDir().getPath();
        } else {
            cachePath = externalCacheDir.getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    File file = new File(cachePath + File.separator + uniqueName);
    if (!file.exists() && !file.isDirectory()) {
        file.mkdir();
    }
    return file;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Get SDCard cache dir.//ww  w. ja v  a  2s  .com
 *
 * @param context
 * @return
 */
public static String getSdCacheDir(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        java.io.File fExternalStorageDirectory = Environment.getExternalStorageDirectory();
        java.io.File autonaviDir = new java.io.File(fExternalStorageDirectory, "move");
        boolean result = false;
        if (!autonaviDir.exists()) {
            result = autonaviDir.mkdir();
        }
        java.io.File minimapDir = new java.io.File(autonaviDir, "offlineMap");
        if (!minimapDir.exists()) {
            result = minimapDir.mkdir();
        }
        return minimapDir.toString() + File.separator;
    } else {
        return "";
    }
}

From source file:Main.java

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

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/*from   ww w  .  j  a v  a2s  .  c o m*/
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) && context.getExternalCacheDir() != null
            && !TextUtils.isEmpty(context.getExternalCacheDir().getPath())) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}