- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 SO 新手,也是 Android 编程新手!如果问题很微不足道,请原谅我。
无论如何,我正在尝试创建一个图库应用程序,它将显示文件夹中所有媒体文件(视频和图片)的缩略图。我的应用程序运行良好,除了处理图像以获取其采样位图的等待时间较长之外。
我已经使用内存缓存(LruCache)解决了这个问题,但它只能解决用户停留在应用程序中并四处导航时的问题。每次用户关闭应用程序并重新打开它时,它都会重新生成缓存。
在网上阅读了一些资料后,我决定在内存缓存的基础上添加磁盘缓存,希望能解决问题。但即使我实现了之后,问题仍然存在。不确定我是否做错了。
下面是我初始化内存缓存和磁盘缓存的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
if (savedInstanceState != null) {
displayManager.cancelAsyncTask();
path = savedInstanceState.getString(ARG_POSITION);
}else{
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}
};
Log.d("DEBUG_CACHE","context is "+this);
File cacheDir = getDiskCacheDir(this, DISK_CACHE_SUBDIR);
Log.d("DEBUG_CACHE", "cache dir is "+cacheDir.getAbsolutePath());
new InitDiskCacheTask().execute(cacheDir);
}
ArrayList<File> folderList = dataManager.getFolderListWithPhotoOrVideo(path);
ArrayList<File> thumbList = dataManager.getAllPhotosAndVideos(path);
displayManager.setCurrentPath(path);
displayManager.setParent(this);
displayManager.setMemCache(mMemoryCache);
displayManager.setFolderList(folderList);
displayManager.setThumbnails(thumbList);
}
下面是我如何从缓存中获取缩略图(如果存在)以及如何将位图添加到缓存中。
@Override
protected Void doInBackground(Void... params) {
int noOfThumbnails = thumbnails.size();
for(int j=0;j<noOfThumbnails;j++){
String filePath = thumbnails.get(j).getName();
if(isPhoto(filePath)){
//if file is an image file
Uri uri = Uri.fromFile(new File(thumbnails.get(j).getAbsolutePath()));
Bitmap thumb = getBitmapFromMemCache(filePath);
if(thumb==null){
thumb = getBitmapFromDiskCache(filePath);
if(thumb==null){
thumb = getPreview(uri);
addBitmapToCache(filePath,thumb);
}
}
bitmaps.add(thumb);
}else{
Bitmap thumb = getBitmapFromMemCache(filePath);
if(thumb==null){
thumb = getBitmapFromDiskCache(filePath);
if(thumb==null){
thumb = ThumbnailUtils.createVideoThumbnail(thumbnails.get(j).getAbsolutePath(),MediaStore.Images.Thumbnails.MICRO_KIND);
addBitmapToCache(filePath,thumb);
}
}
bitmaps.add(thumb);
}
double progress = ((double)(j+1)/(double)noOfThumbnails)*100;
publishProgress(new Double(progress).intValue());
if(isCancelled()){
Log.d("DEBUG", "doInBackGround. Task cancelled");
return null;
}
}
return null;
}
public void addBitmapToCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
// Also add to disk cache
synchronized (parent.mDiskCacheLock) {
try {
if (parent.mDiskLruCache != null && parent.mDiskLruCache.get(key) == null) {
parent.mDiskLruCache.put(key, bitmap);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Bitmap getBitmapFromMemCache(String key) {
return (Bitmap) mMemoryCache.get(key);
}
public Bitmap getBitmapFromDiskCache( String key ) {
Bitmap bitmap = null;
Snapshot snapshot = null;
try {
snapshot = parent.mDiskLruCache.get( key );
if ( snapshot == null ) {
return null;
}
final InputStream in = snapshot.getInputStream( 0 );
if ( in != null ) {
final BufferedInputStream buffIn =
new BufferedInputStream( in, Utils.IO_BUFFER_SIZE );
bitmap = BitmapFactory.decodeStream( buffIn );
}
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( snapshot != null ) {
snapshot.close();
}
}
return bitmap;
}
对于 DiskLruCache.java,我使用了 google 源文件(可以找到 here )。
最佳答案
好的!我终于明白了。 (我自己的愚蠢错误......grrr)
在此之前,我修改了 DiskLruCache.java,以便它验证任何 key !默认情况下,它应该只验证任何带有正则表达式 [a-z0-9_-] 的字符串,也就是说,不应该有任何大写字母、空格,甚至换行符。我认为这没什么大不了的,所以我将其关闭并接受所有字符串。
但是错了!我将其修改回默认值,然后每次添加或获取缓存时都修改我的 key ,完美!
谢谢! :)
关于java - Android:应用程序关闭后 DiskLruCache 未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18844718/
我在这个实现中使用 JakeWharton 的 DiskLruCache:Using DiskLruCache in android 4.0 does not provide for openCach
我是 SO 新手,也是 Android 编程新手!如果问题很微不足道,请原谅我。 无论如何,我正在尝试创建一个图库应用程序,它将显示文件夹中所有媒体文件(视频和图片)的缩略图。我的应用程序运行良好,除
这是我正在关注的磁盘缓存教程。我已将源代码下载到 DiskLruCache,但源代码中不存在本示例中使用的所有方法。 http://developer.android.com/training/dis
我想在我基于 API 级别 7+ 的 Android 应用程序中使用 Jake Wharton 的 DiskLruCache。我会在我的 ListView 中使用它来缓存 SdCard 上下载的图像,
我想在我的项目中使用 DiskLruCache,如 Caching Bitmaps 中所述。 ,但无法导入正确的库。 在哪里可以找到包含此类的 jar? 按照 similar questions 中的
我正在使用 Jake Wharton's DiskLruCache图书馆。 我对应用程序性能、缓存策略以及在 View 中和整个应用程序中使用缓存感到好奇。大多数时候,图像不会改变。 例如,假设我的服
我正在尝试使用 jake wharton DiskLruCache 库实现基于磁盘的图像 lru 缓存。 link to library .我正在使用 here 中的代码 fragment . 我遇到
我收到此错误 java.lang.IllegalArgumentException:键必须匹配正则表达式 [a-z0-9_-]{1,64}:“mision_imposible_–_rogue_nati
我正在尝试在我的 Android 应用程序中使用 Jake Wharton 的 DiskLruCache,但我似乎无法弄清楚如何使用缓存正确序列化和反序列化对象。在基本命令行 Java 程序中使用以下
这与 this 有关关于 Volley 图像缓存的问题。所以,现在我想实现 DiskLruCache,但我不确定该怎么做。 我从 github 下载了 Jar 文件并将其添加到我的项目中。 接下来我该
我正在使用DiskLruCache缓存图像的 url。我使用 AsyncTask 下载图像像这样: new AsyncTask() { String path = null;
在我的 Android 应用程序中,我想在磁盘上缓存图像,然后这些图像将显示在 WebView 中。 ,为此我需要通过某种路径或 URI 访问文件。 (为了使用 。)我发现的所有缓存实现都不提供对绝
我正在尝试在一个新的 Android 项目中测试这个缓存库。 当我运行项目时,我得到这个 Logcat: 01-24 03:45:31.109: E/AndroidRuntime(1983): FAT
有人在 DiskLruCache 上工作过吗? http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html 提
我正在尝试构建 Xamarin.Forms 项目,但出现此错误: Severity Code Description Project File Line Suppression
我是一名优秀的程序员,十分优秀!