Loading images in GridView using Universal Image Loader

GVillani82 picture GVillani82 · Sep 12, 2013 · Viewed 9.7k times · Source

I'm using the Universal Image Loader 1.8.6 library for loading dinamically images taken from web.

The ImageLoaderConfiguration configuration is the following:

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();

and the DisplayImageOptions configuration is:

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

The method getView inside my ArrayAdapter contains the code:

iv.setImageResource(R.drawable.no_foto); 
imageLoader.displayImage(basePath+immagine, iv);

The first line of the above code is used just for avoid that the recycling of view in gridView allow to set image in the wrong position (until the picture has not been downloaded).

The actual problem is this:

If I open my Activity (containing the GridView) the shown items thanks to the UIL library can download the image. (Meanwhile the no_foto.png image is shown in each view of the grid). When all the views have loaded their own images, if I try to scroll down and then I come back (scrolling up) I have to wait that the images are reloaded, because all the views are now occupied by the no_foto.png image.

How can I avoid the reload of these images? Using Universal Image Loader, can I cache the images previous loaded?

NOTE: Since my grid can contain many images I would to use an implementation of disk cache (not memory cache). I found that .discCacheFileCount() can be used for setting the maximum number of cached files, so why it seems that my files are not cached?

Answer

GVillani82 picture GVillani82 · Sep 12, 2013

I have solved the problem

I was just declaring option, but I wans't using it, so I have modified the line:

imageLoader.displayImage(basePath+immagine, iv);

into:

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

and I have added in the options the method:

.cacheOnDisc(true)