Reputation: 10891
I have a ListView with an adapter that loads various data from an API. One of the data is a URL to an image online. I download the image and set it in my ImageView in the adapter.
The problem is though, when I scroll up and down in the ListView, images are re-downloaded and there is a significant lag when doing that. Also, for a split second, it incorrectly displays the wrong image.
I need to somehow cache these images to only download once and properly set them in that ListView - How do I do that?
EDIT: I use universal image loader to load my images like this:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageUrl, viewHolder.thumbnailImageView);
But it does not seem to cache them... Do I have to do something additional?
Upvotes: 0
Views: 216
Reputation: 3356
Use ImageLoaders
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
Simple Image load. Check the link you can use them with listeners.
You can also try:
Android-Universal-Image-Loader
EDIT: As the OP said he uses UIL .
]You should use Image Aware. You can have your own configuration with high cache memory something like this.
public void initializeImageLoader(final Context sContext) {
final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(sContext)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
.taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(50 * 1024 * 1024)) // default
.memoryCacheSize(50 * 1024 * 1024)
.imageDownloader(new BaseImageDownloader(sContext)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.build();
ImageLoader.getInstance().init(config);
}
Use this initialization before you get instance.
initializeImageLoader(context); ImageLoader imageLoader = ImageLoader.getInstance();
Set Options for your loader.
final DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.noimage)
.showImageOnFail(R.drawable.noimage)
.resetViewBeforeLoading(false) //default
.bitmapConfig(Bitmap.Config.RGB_565)
.cacheInMemory(true)
.cacheOnDisk(false) //default
.build();
And set it to your loader:
final ImageAware imageAware = new ImageViewAware(viewHolder.thumbnailImageView, false);
imageLoader.displayImage(imageUrl, imageAware, options);
Upvotes: 1