我正在使用 nostra 的Universal Image Loader,我正在使用它在 gridview 上加载图像,大小为 100*100,但是当通过网格视图快速滚动时会引发异常(仅用于演示,我在网格适配器上有 13000 个图像) ,虽然库处理异常并且没有通知用户,但下面是异常
06-21 11:13:17.748: E/ImageLoader(2070): No such file or directory
06-21 11:13:17.748: E/ImageLoader(2070): java.io.FileNotFoundException: No such file or directory
06-21 11:13:17.748: E/ImageLoader(2070): at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:145)
06-21 11:13:17.748: E/ImageLoader(2070): at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:612)
06-21 11:13:17.748: E/ImageLoader(2070): at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:607)
06-21 11:13:17.748: E/ImageLoader(2070): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:536)
06-21 11:13:17.748: E/ImageLoader(2070): at android.content.ContentResolver.openInputStream(ContentResolver.java:371)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromContent(BaseImageDownloader.java:177)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:88)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:290)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisk(LoadAndDisplayImageTask.java:273)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:229)
06-21 11:13:17.748: E/ImageLoader(2070): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
06-21 11:13:17.748: E/ImageLoader(2070): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-21 11:13:17.748: E/ImageLoader(2070): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-21 11:13:17.748: E/ImageLoader(2070): at java.lang.Thread.run(Thread.java:856)
到位的代码是:
添加了 WRITE_EXTERNAL_STORAGE
内存和磁盘缓存
UnlimitedDiskCache 用于磁盘缓存
使用最新的库 1.9.2
话虽如此,库崩溃并且不再能够加载图像并重复所有以前的图像,在这种情况下如何重置库并重新开始,以防发生这种崩溃?
下面是ImageLoader的配置
String cacheDirectory=context.getCacheDir()+ImageLoaderConstants.IMAGE_CACHE_DIRECTOY_PATH;
File cacheDir=new File(cacheDirectory);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.delayBeforeLoading(0)
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new SimpleBitmapDisplayer())
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(5)
.threadPriority(Thread.NORM_PRIORITY)
.denyCacheImageMultipleSizesInMemory()
.memoryCacheExtraOptions(1280, 720)
.diskCacheExtraOptions(1280, 720,null)
.memoryCache(new LruMemoryCache(5*1024*1024))
.diskCache(new UnlimitedDiscCache(cacheDir))
.defaultDisplayImageOptions(options)
.build();
更新:
适配器代码没什么大不了的,
imageLoader.displayImage(uriPath, holder.imgThumbnail);
我拥有UIL GitHub的使用部分中定义的所有配置