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 GetSdcardSystemPath() {
    String rootpath = "";
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        rootpath = Environment.getExternalStorageDirectory().toString();
    }/*w  w w.  ja  va 2  s.  c om*/
    return rootpath;
}

From source file:Main.java

/** Create a File for saving an image or video */
public static File getOutputMediaFile(int type) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        return null;

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MacauFood");
    // 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("MacauFood", "failed to create directory");
            return null;
        }//from w w w .j  a  va 2  s .  c o  m
    }

    // 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

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted. Else - Android defines cache directory on
 * device's file system.//from   ww w. ja  va 2s  .  c o  m
 * 
 * @param context Application context
 * @return Cache {@link File directory}
 */
public static File getCacheDirectory(Context context) {
    File appCacheDir = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:Main.java

private static File createMediaFile(Context context, String parentPath) {
    String state = Environment.getExternalStorageState();
    File rootDir = state.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory()
            : context.getCacheDir();//from   www.  j  ava2 s  . c o m

    File folderDir = new File(rootDir.getAbsolutePath() + parentPath);
    if (!folderDir.exists() && folderDir.mkdirs()) {

    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
    String fileName = APP_NAME + "_" + timeStamp + "";
    File tmpFile = new File(folderDir, fileName + POSTFIX);
    return tmpFile;
}

From source file:Main.java

public static File getCacheDirectory(Context context, boolean preferExternal, String dirName) {
    File appCacheDir = null;/*from w  w  w.  j ava  2s.co  m*/
    if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, dirName);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

public static File getCacheDirectory(Context context, boolean preferExternal, String dirName) {
    File appCacheDir = null;/*from  w w  w. j  a v a  2s.c  o m*/
    if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, dirName);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        Log.w("Can't define system cache directory! '%s' will be used.", cacheDirPath);
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Helper method to initiate cache directory. It will return the cache directory in File format,
 * or NULL if the directory path is invalid or not accessible.
 *//*from   w w  w  .j av  a  2 s . com*/
public static File getCacheDirectory(final Context context, final String path) {
    File cacheDir = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {
            cacheDir = context.getExternalCacheDir();
        } catch (NullPointerException e) {
            // Fallback to use internal storage if external storage isn't available.
        }
    }
    if (cacheDir == null) {
        cacheDir = context.getCacheDir();
    }
    return (cacheDir != null && path != null) ? new File(cacheDir, path) : null;
}

From source file:Main.java

public static File getAlbumDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File storageDir = new File(dir, "MyAlbum");

        if (!storageDir.mkdirs()) {
            if (!storageDir.exists()) {
                return null;
            }//from  ww w .  j a v a 2s . c o  m
        }

        return storageDir;

    } else {
        return null;
    }
}

From source file:Main.java

public static File createTmpFile(Context context) throws IOException {
    File dir = null;/*from   w  w w  .ja va  2 s .  com*/
    if (TextUtils.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
        dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        if (!dir.exists()) {
            dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
            if (!dir.exists()) {
                dir = getCacheDirectory(context, true);
            }
        }
    } else {
        dir = getCacheDirectory(context, true);
    }
    return File.createTempFile(JPEG_FILE_PREFIX, JPEG_FILE_SUFFIX, dir);
}