Skip to content

Instantly share code, notes, and snippets.

@Bencodes
Created December 11, 2012 07:20
Show Gist options
  • Save Bencodes/4256543 to your computer and use it in GitHub Desktop.
Save Bencodes/4256543 to your computer and use it in GitHub Desktop.

Revisions

  1. Ben Lee revised this gist Dec 11, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions StorageUtil.java
    Original file line number Diff line number Diff line change
    @@ -47,10 +47,10 @@ public static File getStorageDirectory (final Context context, final Storage sto
    switch (storage) {
    case EXTERNAL:
    case EXTERNAL_CACHE: {
    // Check If External Even Exists
    // Check If External Even Exists
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    directory = (storage == Storage.EXTERNAL)
    ? context.getExternalFilesDir(null)
    ? Environment.getExternalStorageDirectory()
    : context.getExternalCacheDir();
    break;
    }
  2. Ben Lee revised this gist Dec 11, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion StorageUtil.java
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ public static enum Storage {
    * @param subDirectory A sub directory path just in case you want to get a sub directory
    * @return
    */
    public static File getStorageDirectory (final Context context, final Storage storage, String subDirectory) {
    public static File getStorageDirectory (final Context context, final Storage storage, final String subDirectory) {
    final File directory;

    switch (storage) {
  3. Ben Lee created this gist Dec 11, 2012.
    81 changes: 81 additions & 0 deletions StorageUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    public final class StorageUtil {

    public static enum Storage {

    /**
    * /data/data/com.my.package/data/files/
    */
    INTERNAL,

    /**
    * (Varies on manufacturer)
    * /mnt/sdcard/
    * /sdcard/
    * <p/>
    * etc...
    */
    EXTERNAL,

    /**
    * /data/data/com.my.package/data/cache/
    */
    INTERNAL_CACHE,

    /**
    * (Varies on manufacturer)
    * /mnt/sdcard/Android/data/com.my.package/cache/
    * /sdcard/Android/data/com.my.package/cache/
    * <p/>
    * etc...
    */
    EXTERNAL_CACHE;
    }

    /**
    * A simple all-in-one method for getting File paths in Android.
    * If you request EXTERNAL/EXTERNAL_CACHE and there is no mounted external
    * storage, it will automatically default to internal.
    *
    * @param context Context so that it can request the appropriate File path.
    * @param storage Enum value for the preferred location (External/Internal)
    * @param subDirectory A sub directory path just in case you want to get a sub directory
    * @return
    */
    public static File getStorageDirectory (final Context context, final Storage storage, String subDirectory) {
    final File directory;

    switch (storage) {
    case EXTERNAL:
    case EXTERNAL_CACHE: {
    // Check If External Even Exists
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    directory = (storage == Storage.EXTERNAL)
    ? context.getExternalFilesDir(null)
    : context.getExternalCacheDir();
    break;
    }
    }

    case INTERNAL: {
    directory = context.getFilesDir();
    break;
    }

    case INTERNAL_CACHE: {
    directory = context.getCacheDir();
    break;
    }

    default: {
    return null;
    }
    }

    return new File(directory, subDirectory);
    }

    public static File getStorageDirectory (final Context context, final Storage storage) {
    return getStorageDirectory(context, storage, null);
    }

    }