Loading images in GridView using Universal Image Loader
Asked Answered
R

2

9

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?

Rosariorosarium answered 12/9, 2013 at 12:51 Comment(12)
UIL works fine, did you try to use postinvalidate() on the image view after the download ? (note that i have the same issu in one activity/list out of 3)Chert
@Joseph82 can u pls tell me how u resolve i m also facing same ? but i m showing images from sdcardGynecoid
@Gynecoid look my answer below: https://mcmap.net/q/1147058/-loading-images-in-gridview-using-universal-image-loaderRosariorosarium
@Joseph82 can u pls tell me how will i write options??? i have declare options but i havennot write anything in optionsGynecoid
DisplayImageOptions options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.no_foto) .showImageForEmptyUri(R.drawable.no_foto) .showImageOnFail(R.drawable.no_foto) .build(); using the drawable you want, instead of no_fotoRosariorosarium
@Joseph82 ok i have edit my code trying to runGynecoid
@Joseph82 i have to first show images in thumbnail then onclick of thumbnail can i get image path ???Gynecoid
@Joseph82 i have run the code its not setting the image from my path its by default setting ic_launcher image which i set in optionsGynecoid
@Joseph82 i m getting this error 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 doesn't support scheme(protocol) by default [/storage/sdcard0/FOLDERNAME/FOLDERNAME_20141215_205027.jpg]. You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))Gynecoid
yes there are 4 images and its also showing me path of that image but this is the error what i posted aboveGynecoid
Why are you trying to use UIL for showing images that are on your disk?Rosariorosarium
Let us continue this discussion in chat.Gynecoid
R
10

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)
Rosariorosarium answered 12/9, 2013 at 13:6 Comment(0)
R
7

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

Regularize answered 12/9, 2013 at 13:6 Comment(1)
I want to set different Image stub for all items in gridview so, how can i do it?Toucan

© 2022 - 2024 — McMap. All rights reserved.