Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading images in GridView using Universal Image Loader

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?

like image 927
GVillani82 Avatar asked Sep 12 '13 12:09

GVillani82


2 Answers

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)
like image 109
GVillani82 Avatar answered Nov 03 '22 16:11

GVillani82


Caching is not enabled by default in UIL, so if you want use the cache you should use

// 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

And while loading image use:

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

Another way is

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

And you can find more information here

like image 25
Isaiah Avatar answered Nov 03 '22 16:11

Isaiah