Skip to content

Instantly share code, notes, and snippets.

@johnkil
Last active February 9, 2021 21:13
Show Gist options
  • Save johnkil/4326097 to your computer and use it in GitHub Desktop.
Save johnkil/4326097 to your computer and use it in GitHub Desktop.
File Util's. Set of basic static final methods for working with the file system on Android OS.
import java.io.File;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
/**
* File Util's.
*
* @author Evgeny Shishkin
*
*/
public class FileUtils {
private static final String LOG_TAG = FileUtils.class.getSimpleName();
/**
* Invisible constructor.
*/
private FileUtils() {
}
/**
* Returns application files directory. Files directory will be created on SD card
* <i>("/Android/data/[app_package_name]/files")</i> if card is mounted.
* Else - Android defines files directory on device's file system.
*
* @param context Application context
* @return Files {@link File directory}
*/
public static File getFilesDir(Context context) {
File filesDir = null;
if (isExternalStorageWritable()) {
filesDir = getExternalFilesDir(context);
}
if (filesDir == null) {
filesDir = getInternalFilesDir(context);
}
return filesDir;
}
/**
* 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.
*
* @param context Application context
* @return Cache {@link File directory}
*/
public static File getCacheDir(Context context) {
File cacheDir = null;
if (isExternalStorageWritable()) {
cacheDir = getExternalCacheDir(context);
}
if (cacheDir == null) {
cacheDir = getInternalCacheDir(context);
}
return cacheDir;
}
/**
* Returns the absolute path to the directory on the external filesystem
* (that is somewhere on Environment.getExternalStorageDirectory()) where the
* application can place persistent files it owns.
*
* @param context
* @return Returns the path of the directory holding application files on external storage.
* Returns null if external storage is not currently mounted so it
* could not ensure the path exists; you will need to call this method again when it is available.
*/
@TargetApi(Build.VERSION_CODES.FROYO)
private static File getExternalFilesDir(Context context) {
File filesDir;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
filesDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + context.getPackageName() + "/files/");
if (!filesDir.exists() && !filesDir.mkdirs()) {
Log.w(LOG_TAG, "Unable to create external files directory");
return null;
}
} else {
filesDir = context.getExternalFilesDir(null);
}
return filesDir;
}
/**
* Returns the absolute path to the directory on the external filesystem
* (that is somewhere on Environment.getExternalStorageDirectory() where the
* application can place cache files it owns.
*
* @param context
* @return Returns the path of the directory holding application cache files on external storage.
* Returns null if external storage is not currently mounted
* so it could not ensure the path exists; you will need to call this method again when it is available.
*/
@TargetApi(Build.VERSION_CODES.FROYO)
private static File getExternalCacheDir(Context context) {
File cacheDir;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
cacheDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + context.getPackageName() + "/cache/");
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
Log.w(LOG_TAG, "Unable to create external cache directory");
return null;
}
} else {
cacheDir = context.getExternalCacheDir();
}
return cacheDir;
}
/**
* Returns the absolute path to the directory on the filesystem where
* files created with {@link #openFileOutput} are stored.
*
* @param context
* @return Returns the path of the directory holding application files on internal storage.
*/
private static File getInternalFilesDir(Context context) {
File filesDir = context.getFilesDir();
return filesDir;
}
/**
* Returns the absolute path to the application specific cache directory
* on the internal filesystem.
*
* @param context
* @return Returns the path of the directory holding application cache files on internal storage.
*/
private static File getInternalCacheDir(Context context) {
File cacheDir = context.getCacheDir();
return cacheDir;
}
/**
* Checks if external storage is available for read and write.
*
* @return true if external storage is available for read and write else false
*/
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* Checks if external storage is available to at least read.
*
* @return true if external storage is available to at least read else false
*/
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
}
@commonsguy
Copy link

I would recommend:

  • Using @TargetApi(NN) (with an appropriate value of NN) instead of @SuppressLint("NewApi")
  • Not mixing your code that requires Apache Commons IO with the rest of the code (as people will grab your gist and get a bunch of compile errors, then perhaps lose trust in the rest of your code)

@johnkil
Copy link
Author

johnkil commented Feb 18, 2013

Thanks for very useful comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment