Example usage for android.os Environment MEDIA_MOUNTED

List of usage examples for android.os Environment MEDIA_MOUNTED

Introduction

In this page you can find the example usage for android.os Environment MEDIA_MOUNTED.

Prototype

String MEDIA_MOUNTED

To view the source code for android.os Environment MEDIA_MOUNTED.

Click Source Link

Document

Storage state if the media is present and mounted at its mount point with read/write access.

Usage

From source file:Main.java

public static boolean checkExternalDispose() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    final 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 {//from ww  w. java 2  s.c  o m
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    return mExternalStorageAvailable && mExternalStorageWriteable;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *//*www  . j  a v a  2  s  . co m*/
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    // Edit: store in DIRECTORY_DCIM
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "CameraSample");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *///from ww  w.j  a  v a  2s  . c o m
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "mapiary");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *///from  ww  w. j av  a  2s.c o m
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraSample");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *//*from  w  ww.  j  av  a 2 s.c  om*/
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
            "waivelength");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *//*from w  w w. j  av a 2 s .  co m*/
public static File getOutputMediaFile(int type, String token, String extension) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FloatingWindow2");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + "-" + token + extension);
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *//*from  w  w  w  .j  a v  a  2s .c o  m*/
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraSample");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    @SuppressLint("SimpleDateFormat")
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *//*from w w  w  .j a  va  2  s  .c  om*/
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraSample");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraSample", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else if (type == MEDIA_TYPE_SENSOR) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".dat");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *///  w ww.j  av a  2 s  .c  o m
public static File getTempOutputMediaFile(Context context, int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    @SuppressLint("SimpleDateFormat")
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    if (type == MEDIA_TYPE_IMAGE) {
        timeStamp += ".jpg";
    } else if (type == MEDIA_TYPE_VIDEO) {
        timeStamp += ".mp4";
    } else {
        return null;
    }

    try {
        return new File(context.getExternalCacheDir().getAbsolutePath(), timeStamp);
    } catch (NullPointerException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getAvailableStoragePath(Context context) {
    if (BASE_DIR == null) {
        try {/*from  ww  w  .ja v a  2s. c o  m*/
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
            StorageManager sStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

            Class<?> smClazz = sStorageManager.getClass();
            Method listMethod = smClazz.getDeclaredMethod("getVolumeList");
            Object vlObject = listMethod.invoke(sStorageManager);

            if (vlObject.getClass().isArray()) {
                String state = null;
                String path = null;
                // Class svClazz =
                // Class.forName("android.os.storage.StorageVolume");
                Object svObject = Array.get(vlObject, 1);
                if (svObject != null) {
                    Method pathMethod = svObject.getClass().getMethod("getPath");
                    path = (String) pathMethod.invoke(svObject);

                    Method stateMethod = smClazz.getMethod("getVolumeState", new Class[] { String.class });
                    state = (String) stateMethod.invoke(sStorageManager, path);
                }

                if (path != null && state != null && state.equals(Environment.MEDIA_MOUNTED)) {
                    BASE_DIR = path;
                } else {
                    BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
                }
            }
        } catch (Exception e) {
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

        return BASE_DIR;
    } else {
        return BASE_DIR;
    }

}