Search code examples
androidcachinguniversal-image-loader

Fix warning of Universal Image Loader: "Try to initialize ImageLoader which had already been initialized before. Universal image loader"


My logcat gives me this message whenever I load an image in my application.

04-09 19:09:59.241: W/ImageLoader(276): 
Try to initialize ImageLoader which had already been initialized before. 
To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.

I do not understand why. Here is my code:

//If save on disk setting is false, do not save on disk. else, save on disk.
    if (dataReturned =="false"){
        defaultOptions = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisk(false)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();
    }else{
        defaultOptions = new DisplayImageOptions.Builder()
        .cacheOnDisc(true)
        .cacheInMemory(true)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();
    }
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .denyCacheImageMultipleSizesInMemory()
        .build();
ImageLoader.getInstance().init(config); // Do it on Application start

This is someone else with the same problem, but it did not help me Fix a warning of ImageLoader : "Try to initialize ImageLoader which had already been initialized before" since I do only initialize the ImageLoader onCreate, only once. While he initialized it over and over again.


Solution

  • I'm assuming you have it on a Activity.

    if you are going to put it in there, you would have to call ImageLoader.destroy on onDestroy like this:

    protected void onDestroy() {
        ImageLoader.getInstance().destroy();
    }
    

    You could put the initialization in your Application Class http://developer.android.com/reference/android/app/Application.html something like this:

    YourApplication extends Application {
        protected void onCreate() {
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .denyCacheImageMultipleSizesInMemory()
            .build();
            ImageLoader.getInstance().init(config);
        }
    }