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

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

Related

Image caching using Universal Image loader in 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);

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

How to use universal image loader to download raw images WITHOUT DECODING

I want to download images with universal image loader but I do not want it to decode the images and create bitmap from it. I just want to save the original image in a directory. how can I disabling it to decode?
Can I use other libraries? for example picasso but I think it can not do that, am I right?
I think the answer would be:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.NONE)
.build();
because of:
public enum ImageScaleType {
/** Image won't be scaled */
NONE,
from
https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java#L24

Where is the cache folder of the Universal ImageLoader?

The caching of my Universal ImageLoader seems not working. I'm wondering where the folder of the cache is. I found there is a folder in my sdcard:
data->data->id of my app->cache->uil-images
But it's empty. Here is my code:
DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.cacheInMemory(false)
.showStubImage(R.drawable.img_placeholder)
.showImageForEmptyUri(R.drawable.img_placeholder)
.showImageOnFail(R.drawable.img_placeholder)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(500))
.build();
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this.context));
imageLoader.displayImage(imgUrl, imageView, displayOptions);
No matter I set the cacheInMemory to false or true, the result is the same. Can somebody explain what could be wrong with it? Thanks.
By the way, I'm using universal imageloader 1.8.6
According to the source code file library/src/main/java/com/nostra13/universalimageloader/utils/StorageUtils.java, cache directory will be created on :
SD card, in preference, if card is mounted and app has appropriate permission, at this location /Android/data/[app_package_name]/cache/
device's file system /data/data/[app_package_name]/cache/.
This response need to completed because incomplete.

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