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 void openCamera(Context context, int code) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT,
                context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new ContentValues()));
        // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory() + imagePath)));
        ((Activity) context).startActivityForResult(intent, code);

    } else/*from ww  w  .  j  av a  2s . c om*/
        Toast.makeText(context, "no sdcard!", Toast.LENGTH_SHORT).show();
}

From source file:Main.java

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

From source file:Main.java

public static String getBitmapStoragePath(Context context) {
    String bitmapStoragePath = "";
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {//from  ww w  .j a  v  a2  s.c om
            String root = context.getExternalFilesDir(null).getCanonicalPath();
            if (null != root) {
                File bitmapStorageDir = new File(root, APP_DIR);
                bitmapStorageDir.mkdirs();
                bitmapStoragePath = bitmapStorageDir.getCanonicalPath();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return bitmapStoragePath;
}

From source file:Main.java

public static String getSaveImagePath(Context context) {
    String path = getCacheDir(context).getAbsolutePath();
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        path = Environment.getExternalStorageDirectory().getAbsolutePath();
    }/*from w ww .jav a 2 s  .  com*/

    path = path + File.separator + "Pictures";
    File file = new File(path);
    if (!file.exists()) {
        file.mkdir();
    }
    return path;
}

From source file:Main.java

public static boolean isExternalMemoryPresent() {
    String externalStorageState = Environment.getExternalStorageState();
    return externalStorageState.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}

From source file:Main.java

public static boolean isExternalMemoryWritable() {
    String externalStorageState = Environment.getExternalStorageState();
    return externalStorageState.equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();//from w w w .  java2 s .c om
        }
    }

    File file = new File(path);
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();//from  w w w .ja  v a  2s .c o m
        }
    }

    File file = new File(path);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean isExternalStoragePresent(Context context) {
    boolean externalStorageAvailable;
    boolean externalStorageWritable;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        externalStorageAvailable = externalStorageWritable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        externalStorageAvailable = true;
        externalStorageWritable = false;
    } else {/*from w ww. j ava  2 s.c om*/
        // 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
        externalStorageAvailable = externalStorageWritable = false;
    }
    if (!((externalStorageAvailable) && (externalStorageWritable))) {
        Toast.makeText(context, "SD card not present", Toast.LENGTH_LONG).show();
    }
    return (externalStorageAvailable) && (externalStorageWritable);
}

From source file:Main.java

/**
 * Get the external storage path of the device
 *
 * @return The external storage path of the device.
 *///from   www. ja  v a2s . com
public static String getAppRootDirectory() {
    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() + "/" + ROOT_DIR + "/";
    File file = new File(rootDir);
    if (!file.exists()) {
        if (!file.mkdirs()) {
            return null;
        }
    }
    return rootDir;
}