Skip to content

Instantly share code, notes, and snippets.

@johnkil
Created August 11, 2012 10:40
Show Gist options
  • Save johnkil/3323708 to your computer and use it in GitHub Desktop.
Save johnkil/3323708 to your computer and use it in GitHub Desktop.
Getting external files/cache dir [compatibility]
/**
* Getting external files/cache dir [compatibility].
*
* @author johnkil
*
*/
public class FileUtils {
private static final String LOG_TAG = FileUtils.class.getSimpleName();
/**
* 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.
*/
@SuppressLint("NewApi")
public static File getExternalFilesDir(Context context) {
Log.v(LOG_TAG, "getExternalFilesDir() called");
File dir;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "/Android/data/" + context.getPackageName() + "/files/");
if (!dir.exists() && !dir.mkdirs())
dir = null;
} else {
dir = context.getExternalFilesDir(null);
}
return dir;
}
/**
* 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.
*/
@SuppressLint("NewApi")
public static File getExternalCacheDir(Context context) {
Log.v(LOG_TAG, "getExternalCacheDir() called");
File dir;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "/Android/data/" + context.getPackageName() + "/cache/");
if (!dir.exists() && !dir.mkdirs())
dir = null;
} else {
dir = context.getExternalCacheDir();
}
return dir;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment