下面列出了android.content.Context#getExternalCacheDirs ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathToNonPrimaryVolume(Context context, String tag) {
File[] volumes = context.getExternalCacheDirs();
if (volumes != null) {
for (File volume : volumes) {
if (volume != null) {
String path = volume.getAbsolutePath();
if (path != null) {
int index = path.indexOf(tag);
if (index != -1) {
return path.substring(0, index) + tag;
}
}
}
}
}
return null;
}
private static String getPathToNonPrimaryVolume(Context context, String tag) {
File[] volumes = context.getExternalCacheDirs();
if (volumes != null) {
for (File volume : volumes) {
if (volume != null) {
String path = volume.getAbsolutePath();
if (path != null) {
int index = path.indexOf(tag);
if (index != -1) {
return path.substring(0, index) + tag;
}
}
}
}
}
return null;
}
public static List<File> getCacheDirs(Context context) {
final ArrayList<File> dirs = new ArrayList<>();
dirs.add(context.getCacheDir());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
for(final File dir : context.getExternalCacheDirs()) {
if(dir != null) {
dirs.add(dir);
}
}
} else {
final File extDir = context.getExternalCacheDir();
if (extDir != null) {
dirs.add(extDir);
}
}
return dirs;
}
public static void setCacheDir(Context context) {
File maxDir = null;
long maxSpace = -1;
String path;
File[] dirs = context.getExternalCacheDirs();
for (int k = 0; k < dirs.length; ++k) {
File dir = dirs[k];
long dirFreeSpace = dir.getFreeSpace();
// testing:
path = dir.getAbsolutePath();
Log.d(TAG, "- #" + k + " cache path: " + path);
if (dirFreeSpace > maxSpace) {
maxSpace = dirFreeSpace;
maxDir = dir;
}
}
if (maxDir != null) {
path = maxDir.getAbsolutePath();
Log.d(TAG, "- best cache path: " + path);
Magick.setCacheDir(path);
} else
Log.d(TAG, "- best cache dir null");
}
public static File[] getExternalCacheDirs(Context context) {
if (Build.VERSION.SDK_INT >= 19) {
return context.getExternalCacheDirs();
} else {
return new File[] {context.getExternalCacheDir()};
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static ArrayList<String> listSelectionPathsLollipop(Context context, boolean cacheDir)
{
File files[];
if (cacheDir)
{
files = context.getExternalCacheDirs();
}
else
{
files = context.getExternalMediaDirs();
}
final ArrayList<String> items = new ArrayList<String>();
items.add(getDefaultBaseDirectory(context).getPath());
for (File curFile : files)
{
if (curFile == null)
{ // Regarding to android javadoc this may be possible.
continue;
}
String mountState = Environment.getExternalStorageState(curFile);
if (mountState.equals(Environment.MEDIA_MOUNTED) ||
mountState.equals(Environment.MEDIA_SHARED))
{
items.add(curFile.getPath());
}
}
return items;
}
/**
* Parse and return {@link PathStrategy} for given authority as defined in
* {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
*
* @see #getPathStrategy(Context, String)
*/
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData(
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException(
"Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type = in.next()) != END_DOCUMENT) {
if (type == START_TAG) {
final String tag = in.getName();
final String name = in.getAttributeValue(null, ATTR_NAME);
String path = in.getAttributeValue(null, ATTR_PATH);
File target = null;
if (TAG_ROOT_PATH.equals(tag)) {
target = DEVICE_ROOT;
} else if (TAG_FILES_PATH.equals(tag)) {
target = context.getFilesDir();
} else if (TAG_CACHE_PATH.equals(tag)) {
target = context.getCacheDir();
} else if (TAG_EXTERNAL.equals(tag)) {
target = Environment.getExternalStorageDirectory();
} else if (TAG_EXTERNAL_FILES.equals(tag)) {
File[] externalFilesDirs;
if (Build.VERSION.SDK_INT >= 19) {
externalFilesDirs = context.getExternalFilesDirs(null);
} else {
externalFilesDirs = new File[] { context.getExternalFilesDir(null) };
}
if (externalFilesDirs.length > 0) {
target = externalFilesDirs[0];
}
} else if (TAG_EXTERNAL_CACHE.equals(tag)) {
File[] externalCacheDirs;
if (Build.VERSION.SDK_INT >= 19) {
externalCacheDirs = context.getExternalCacheDirs();
} else {
externalCacheDirs = new File[] { context.getExternalCacheDir() };
}
if (externalCacheDirs.length > 0) {
target = externalCacheDirs[0];
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& TAG_EXTERNAL_MEDIA.equals(tag)) {
File[] externalMediaDirs = context.getExternalMediaDirs();
if (externalMediaDirs.length > 0) {
target = externalMediaDirs[0];
}
}
if (target != null) {
strat.addRoot(name, buildPath(target, path));
}
}
}
return strat;
}
public static File[] getExternalCacheDirs(Context context) {
return context.getExternalCacheDirs();
}
public static File[] getExternalCacheDirs(Context context) {
return context.getExternalCacheDirs();
}
public static File[] getExternalCacheDirs(Context context) {
return context.getExternalCacheDirs();
}