使用Universal Image Loader在GridView中加载图片

9
我正在使用Universal Image Loader 1.8.6库来动态加载从网络上获取的图片。 ImageLoaderConfiguration配置如下:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
    .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
    .threadPoolSize(3) // default
    .threadPriority(Thread.NORM_PRIORITY - 1) // default
    .denyCacheImageMultipleSizesInMemory()
    .memoryCacheSize(2 * 1024 * 1024)
    .memoryCacheSizePercentage(13) // default
    .discCacheSize(50 * 1024 * 1024)
    .discCacheFileCount(100)
    .imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
    .writeDebugLogs()
    .build();

并且DisplayImageOptions配置如下:

DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.no_foto)
    .showImageForEmptyUri(R.drawable.no_foto)
    .showImageOnFail(R.drawable.no_foto)
    .build();

我的ArrayAdapter中的getView方法包含以下代码:
iv.setImageResource(R.drawable.no_foto); 
imageLoader.displayImage(basePath+immagine, iv);

以上代码的第一行只是为了避免在 gridView 中回收视图时,导致将图片设置在错误的位置(直到图片未被下载)。
实际问题是:
如果我打开包含 GridView 的 Activity,由于 UIL 库的帮助,显示的项可以下载图片。(同时,在网格视图的每个视图中都显示no_foto.png 图像)。当所有视图都加载完自己的图像后,如果我尝试向下滚动,然后再返回(向上滚动),我必须等待重新加载图像,因为现在所有视图都被 no_foto.png 图像占据了。
如何避免这些图像的重新加载?使用 Universal Image Loader,我可以缓存之前加载的图像吗?
注意:由于我的网格可能包含许多图像,我希望使用磁盘缓存的实现(而不是内存缓存)。我发现 .discCacheFileCount() 可以用于设置缓存文件的最大数量,那么为什么我的文件似乎没有被缓存呢?

UIL运行良好,您是否尝试在图像视图下载后使用postinvalidate()?(请注意,我在3个活动/列表中有一个相同的问题) - An-droid
@Joseph82 好的,我已经编辑了我的代码,正在尝试运行。 - user3233280
@Joseph82 我需要先展示缩略图,然后在缩略图上点击后才能获取图片路径吗? - user3233280
@Joseph82,我已运行代码,但它没有从我的路径设置图像,而是默认设置了我在选项中设置的ic_launcher图像。 - user3233280
@Joseph82 我遇到了这个错误 12-15 21:00:25.267: D/GridViewAdapter(17685): filepath[position]:/storage/sdcard0/FOLDERNAME/FOLDERNAME_20141215_205027.jpg 12-15 21:00:25.277: E/ImageLoader(17685): UIL默认不支持此协议 [/storage/sdcard0/FOLDERNAME/FOLDERNAME_20141215_205027.jpg]。您应该自己实现此支持(BaseImageDownloader.getStreamFromOtherSource(...))。 - user3233280
显示剩余7条评论
2个回答

10

我已经解决了这个问题。

我只是声明了选项,但我没有使用它,所以我修改了这行:

imageLoader.displayImage(basePath+immagine, iv);

将:

imageLoader.displayImage(basePath+immagine, iv, options);

我已经在选项中添加了这种方法:

.cacheOnDisc(true)

7

UIL默认情况下不启用缓存,因此如果您想使用缓存,应该使用:

// Create default options which will be used for every 
//  displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    ...
    .cacheInMemory()
    .cacheOnDisc()
    ...
    .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
    ...
    .defaultDisplayImageOptions(defaultOptions)
    ...
    .build();
ImageLoader.getInstance().init(config); // Do it on Application start

在加载图片时,请使用以下代码:

// Then later, when you want to display image
ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used

另一种方法是

DisplayImageOptions options = new DisplayImageOptions.Builder()
    ...
    .cacheInMemory()
    .cacheOnDisc()
    ...
    .build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options); 

你可以在这里找到更多信息。


我想为GridView中的所有项设置不同的图像占位符,那么我该怎么做? - Krunal Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接