0

I am using universal image loader to load images. The issue is that It does not save loaded images in cache Here is my code.

    Map<String, String> headers     = new HashMap<String, String>();    
    headers.put("key", Commons.CURRENT_ACTIVE_PROFILE.getKey()); 
    headers.put("secret", Commons.CURRENT_ACTIVE_PROFILE.getSecret());  

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
    .imageDownloader(new CustomImageDownaloder(mActivity)).build(); 

    //imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity)); 

    imageLoader.init(config);  


    imageLoader = ImageLoader.getInstance();
    displayImageOptions = new DisplayImageOptions.Builder()
    .extraForDownloader(headers)
    .showImageForEmptyUri(R.drawable.no_preview)
    .showImageOnLoading(R.drawable.no_preview) 
    .showImageOnFail(R.drawable.no_preview)
    .cacheInMemory(true) 
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .build();

The problem is when I use following line for ImageLoaderCinfiguration that creates a default settings It would work fine and saves the images in cache.

imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity));

But I want to use custom settings for ImageLoaderConfiguration because I am using CustomImageDownloader to pass key/secret with URL. So i use following lines for config

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
.imageDownloader(new CustomImageDownaloder(mActivity)).build();

But In this case it doest not save image and if I run the app without internet it does not load the images. Any help/suggestion please?

Possible duplicate I have tried that but no use.

Community
  • 1
  • 1
Nouman Bhatti
  • 1,777
  • 4
  • 28
  • 55
  • a bit off topic but consider switching from UniversalImageLoader to Picasso library (http://square.github.io/picasso/). We have used UIL in several projects before and then switched to picasso. It is really faster than UIL and better (and not too hard to migrate). – Yuraj Aug 23 '14 at 10:55
  • I would try that library but now i cant use it in this project as i have used UIL at several places in my app. Do you have any idea of this issue i have mentioned in post? – Nouman Bhatti Aug 23 '14 at 11:05
  • Also I did some research http://java.dzone.com/articles/be-lazy-productive-android-0 for picasso. Yes it is faster but has some limitations like Picasso doesn’t provide a way to prepare and store thumbnails of local images. t doesn’t provide a callback functionality to check any state. “fetch()” dose not pass back anything. “get()” is for synchronously read, and “load()” is for asynchronously draw a view. And also to speed up image loading you have to trade off some image quality. – Nouman Bhatti Aug 23 '14 at 11:11
  • You can use fetch in loop to download all your thumbnails - yeah It does not return anything but It gets loaded in cache (in memory or disk). So when you next time call load/get you will get image loaded from cache. – Yuraj Aug 24 '14 at 12:45

2 Answers2

3

U can use this simple method for Image Loader view

private void initImageLoader() {
    // ImageLoader initializing
    DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(opts).build();
    ImageLoader.getInstance().init(config);
}
Htut
  • 267
  • 3
  • 18
0

I have resolved the issue myself. as using

.imageDownloader(new CustomImageDownaloder(mActivity)).build(); 

would override the default imageDownloader that is

.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default

Default image downloader class contains diffrent methods to load images from web, local storage, assets etc. But the CustomImageDownaloder that i have created only contains code to get stream from web/network. So what I did is to copy and paste all the code of default BaseImageDownloader to CustomImageDownaloder and just modify the

getStreamFromNetwork(String imageUri, Object extra)

according to my requirements.

Nouman Bhatti
  • 1,777
  • 4
  • 28
  • 55