Skip to content

Instantly share code, notes, and snippets.

@svenporto
Last active August 29, 2015 14:04
Show Gist options
  • Save svenporto/462efdb27130e1aa03a0 to your computer and use it in GitHub Desktop.
Save svenporto/462efdb27130e1aa03a0 to your computer and use it in GitHub Desktop.
Android storage helper enum
public enum AndroidStorage {
/**
* /data/data/com.mypackage.name/files
*/
INTERNAL_APPLICATION,
/**
* /storage/emulated/0
*/
EXTERNAL_ROOT,
/**
* /storage/emulated/0/Android/data/com.mypackage.name/files
*/
EXTERNAL_APPLICATION;
public String getPath(Context p_context) {
switch (this) {
case EXTERNAL_ROOT:
return Environment.getExternalStorageDirectory().getPath();
case EXTERNAL_APPLICATION:
return p_context.getExternalFilesDir(null).getPath();
case INTERNAL_APPLICATION:
return p_context.getFilesDir().getPath();
}
return null;
}
public File getDirectory(Context p_context, String p_directory) {
File file = new File(getPath(p_context), p_directory);
if(!file.exists()) {
file.mkdirs();
}
return file;
}
public static boolean isExternalReadable() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}
public static boolean isExternalWritable() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment