Nouman Bhatti
Nouman Bhatti

Reputation: 1847

android universal image loader not caching or saving image

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.

Upvotes: 0

Views: 2661

Answers (2)

Htut
Htut

Reputation: 265

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

Upvotes: 3

Nouman Bhatti
Nouman Bhatti

Reputation: 1847

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.

Upvotes: 0

Related Questions