我有一个显示来自互联网的图片的应用程序(展示设计师作品)。我开始在内部缓存目录中缓存我的内容,但应用程序内容可能需要大约 150 MB 的缓存大小。什么 android 文档说:
You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
最佳答案
在我回答您的问题之前,以下是对两种存储类型的简要说明:
缓存
这是文件系统上特定于应用程序的目录。此目录的目的是存储您的应用程序可能需要在 session 之间保留的临时数据,但对于永久保留它们可能并不重要。您通常使用 Context.getCacheDir()
访问此目录.这将在您的应用设置中显示为“缓存”。
文件
与缓存目录一样,您的应用程序也有一个用于保存文件的特定于应用程序的目录。此目录中的文件将一直存在,直到应用程序明确删除它们或卸载应用程序。您通常使用 Context.getFilesDir()
访问此目录.这可以在应用程序信息屏幕上显示为各种内容,但在您的屏幕截图中,这是“USB 存储数据”。
注意:如果你想明确地放置在外部媒体(通常是 SD 卡)上,你可以使用 Context.getExternalFilesDir(String type)
.
差异
这两个目录都特定于您的应用程序(其他应用程序无权访问)。缓存目录和文件目录之间的一个区别是,如果系统存储空间不足,它首先要从缓存目录中释放资源。系统不会清除文件目录中的任何数据。另一个区别是缓存目录通常可以从应用程序信息屏幕手动清除。 files 目录通常也可以,但清除 files 目录也会清除缓存目录。
我使用哪一种?
这取决于该数据与您的应用程序的生命周期相比的重要性。如果您只需要一个 session 的数据并且您怀疑您是否需要再次使用该数据,那么不要使用任何一个。只需将其保存在内存中,直到您不需要它为止。如果您怀疑需要在多个 session 之间重用数据,但又不必保留硬拷贝,请使用缓存目录。如果您无论如何都必须拥有这些数据,或者是需要持久存储的相当大的数据,请使用 files 目录。以下是我能想到的一些例子:
Context.getCacheDir()
文档:Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.
public class CacheManager {
private static final long MAX_SIZE = 5242880L; // 5MB
private CacheManager() {
}
public static void cacheData(Context context, byte[] data, String name) throws IOException {
File cacheDir = context.getCacheDir();
long size = getDirSize(cacheDir);
long newSize = data.length + size;
if (newSize > MAX_SIZE) {
cleanDir(cacheDir, newSize - MAX_SIZE);
}
File file = new File(cacheDir, name);
FileOutputStream os = new FileOutputStream(file);
try {
os.write(data);
}
finally {
os.flush();
os.close();
}
}
public static byte[] retrieveData(Context context, String name) throws IOException {
File cacheDir = context.getCacheDir();
File file = new File(cacheDir, name);
if (!file.exists()) {
// Data doesn't exist
return null;
}
byte[] data = new byte[(int) file.length()];
FileInputStream is = new FileInputStream(file);
try {
is.read(data);
}
finally {
is.close();
}
return data;
}
private static void cleanDir(File dir, long bytes) {
long bytesDeleted = 0;
File[] files = dir.listFiles();
for (File file : files) {
bytesDeleted += file.length();
file.delete();
if (bytesDeleted >= bytes) {
break;
}
}
}
private static long getDirSize(File dir) {
long size = 0;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
size += file.length();
}
}
return size;
}
}
https://stackoverflow.com/questions/9942560/
相关文章:
android - 如何在没有硬件加速的情况下在 Windows 8 上为 API 21 和 19
android - NoSuchFieldError : No static field listV
android - transformClassesAndResourcesWithProguard
android - 如何将 Activity 带到前台(堆栈顶部)?
android - 如何使用它自己的操作按钮关闭 Snackbar?
android - Kotlin 错误 : Could not find org. jetbrain