Image caching using Universal Image loader in android - android

I am working on a project in which I have to lazy load profile images of contacts into recyclerview, and I am loading cached images into recyclerview if the user is offline. I am using Universal Image loader to load images from server and cache it in device.Universal Image loader is caching Images in file path
[Android->data->packagename->cache]
here the problem is user able to see the cached images (thumbnails of images) by going to that path [Android->data->packagename->cache].
1) Is there any way that I can cache images with different extension so the user cannot able to see those cached images?
2) How to cache images other than this path [Android->data->packagename->cache] using UIL ?
Here is how I tried to cache image with different extension and in different location,It created folder "cool" but the image is getting cached in default location ([Android->data->packagename->cache])
I tried to cache image in [Android->data->packagename->files]
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.profileimage)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
File cacheDir = this.getExternalFilesDir("cool");
File cache = new File(cacheDir,"cool.m");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(defaultOptions)
.diskCache(new UnlimitedDiskCache(cache))
.build();
ImageLoader.getInstance().init(config);

Related

Different saving path for universal image loader

I am using 'universal image loader' library in my application to load images from internet. In some parts of my application I don't need to cache the images in memory for long time so I clear the cache every 24 hours. In other parts I need to cache images for long time and I don't want to clear them.
My question is, is it possible to config universal image loader some how to use different caching paths and the other question is how can I save the cached data in a custom folder ?
This is how I configure my uil :
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_loading)
.showImageForEmptyUri(R.drawable.strawberry)
.showImageOnFail(R.drawable.image_faild)
.cacheInMemory(false)
.cacheOnDisk(true)
.considerExifParams(true).build();
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
You may use different instances DisplayImageOptions. Something like this
cachedOnDiskOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_loading)
.showImageForEmptyUri(R.drawable.strawberry)
.showImageOnFail(R.drawable.image_faild)
.cacheInMemory(false)
.cacheOnDisk(true)
.considerExifParams(true).build();
cachedInMemoryOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_loading)
.showImageForEmptyUri(R.drawable.strawberry)
.showImageOnFail(R.drawable.image_faild)
.cacheInMemory(true)
.cacheOnDisk(false)
.considerExifParams(true).build();
ImageLoader.getInstance().displayImage(url, imageView, cachedInMemoryOptions);
ImageLoader.getInstance().displayImage(anotherUrl, anotherImageView, cachedOnDiskOptions);

Android - UIL How to show loading image only when image has not cached?

I am using Universal Image Library for displaying images in Android.
I know using showImageOnLoading(img_id) in DisplayImageOptions allows ImageView to show a Loading Image while image resource is loading.
It works great when I use this option for grabbing images from internet.
However, it is poor when it loads images from cache. It shows the Loading Image just an instant, then shows the Cached Image. It is just a blink.
I don't want a blink and just show the Cached Image only(without the Loading Image) if a cached image exists.
Here is the code I used:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.cacheOnDisk(true)
.cacheInMemory(false)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
String url = "http://i2.wp.com/www.fmxexpress.com/wp-content/uploads/2014/02/pullto.jpg";
ImageView image = (ImageView) findViewById(R.id.image);
ImageLoader.getInstance().displayImage(url, image, options);
How could I do that?
I think that blink is caused by the process of reading the file from disk.
What if you just cache the image in memory like this:
.cacheOnDisk(false)
.cacheInMemory(true)

Expire UIL cached images after 2 days in android

I am using Universal Image Library (UIL) to load images in my application.
Now problem is that I can have different images on same URL. I mean today I have a cat picture in a URL and tomorrow I might have a dog picture on same URL. So I want to check that if cached image is 2 days old then delete it and download the image again.
This is my configuration.
File cacheDir = StorageUtils.getOwnCacheDirectory(this, "FriendListCache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
// You can pass your own memory cache implementation
.diskCache(new UnlimitedDiscCache(cacheDir))
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.build();
ImageLoader.getInstance().init(config);
And this is image loading options.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.medium_load)
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.decodingOptions(resizeOptions)
.displayer(new RoundedBitmapDisplayer(160))
.build();
UIL provide LimitedAgeDiscCache option for your requirement.
LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)
.discCache(new LimitedAgeDiscCache(cacheDir, 14400))

How to set universal image loader to download a picture once?

I'm using universal image loader.
I want to download a picture from url and show it in my application.
I'm sure that i'll never change the picture in my site.
So I need it to be downloaded once. and don't even check the date of modification to download it.
In fact I want the app to check if the image was downloaded the don't download it :)
How can I do this.
Thank you
instead of download image you should keep in drawable and use it
if you are download image via UID its save in catch memory and it is removable you can clear that catch once it is clear than you have to download again if you sure that you want download once only than i suggest go for drawable not for UID unless you have specific requirement
On universal loader if you are true cacheInMemory then it maintain cache and your image download once. By default it is false:-
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
...
.cacheInMemory(true)
.cacheOnDisk(true)
...
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
...
.defaultDisplayImageOptions(defaultOptions)
...
.build();
ImageLoader.getInstance().init(config);
for more info you have to read below link:-
https://github.com/nostra13/Android-Universal-Image-Loader

Display Image in list View with using ImageLoader once time

I've code to display image in list view using imageLoader library from com.nostra13.universalimageloader
this code like this
imageLoader.displayImage(link_url, imageView, options, animateFirstListener);
but this code make this app must download image from this url. I wanna ask , can I using this cache image to show image. So if not connect internet the image can be show because this image was save as cache before?? How to show image in imageview if this image is from cache?? thanks
Instead of downloading it again you should cache images in phone memory or sdcard.
It caches images in say sdcard (if you have configured properly) using url as the key. If present display from cache else download, cache and display images.
In your custom adapter constructor
File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "cache_folder")
imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
.discCache(new UnlimitedDiscCache(cacheDir))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image untik image is loaded
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
viewholder.image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, viewholder.image,options);
By deafult it stores cache i guess.
Check once Disconnecting internet.
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.deafaultimage).cacheInMemory()
.cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
HTTP caching depends on Cache-Control headers. If the server is sending the proper headers in the response then the library will automatically cache the resource automatically and access it even if there is no network. You don't have to do anything explicitly.
You can configure more options such as cache size etc if you want. See the docs.

Categories

Resources