1

Below I have posted my application class.I have tried to add this below universal image loader code into Appcontroller.java class.

// UNIVERSAL IMAGE LOADER SETUP
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

        // END - UNIVERSAL IMAGE LOADER SETUP

But I couldn't add this.Because In volley image loader import is different to that universal image loader.

Imports:

Volley ImageLoader - import com.android.volley.toolbox.ImageLoader;

Universal ImageLoader -import com.nostra13.universalimageloader.core.ImageLoader;

If I add Universal Image Loader inside Appcontroller, I am getting compile error in this line ImageLoader.getInstance().init(config);.

Because Two ImageLoader imports are differnet.

AppController .java:(Application Class)

import com.android.volley.toolbox.ImageLoader;

public class AppController extends Application {

    private static int pendingNotificationsCount = 0;

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    LruBitmapCache mLruBitmapCache;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        MultiDex.install(this);

        mInstance = this;




    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            getLruBitmapCache();
            mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
        }

        return this.mImageLoader;
    }

    public LruBitmapCache getLruBitmapCache() {
        if (mLruBitmapCache == null)
            mLruBitmapCache = new LruBitmapCache();
        return this.mLruBitmapCache;
    }

    public void addToRequestQueue(Request<String> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        req.setShouldCache(false);
        getRequestQueue().add(req);
    }

    public void addToRequestQueue(JsonObjectRequest req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }


    public static int getPendingNotificationsCount() {
        return pendingNotificationsCount;
    }

    public static void setPendingNotificationsCount(int pendingNotifications) {
        pendingNotificationsCount = pendingNotifications;
    }

}
Stephen
  • 9,899
  • 16
  • 90
  • 137

2 Answers2

2

Your problem doesn't come from using both UIL and Volley but using 2 classes with same name from different libraries.

You can use full qualified name like com.package.path.ImageLoader instead of import to solve the problem.

You also can check out this question: Importing two classes with same name. How to handle?

Update with demo code

Instead of

    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

    // END - UNIVERSAL IMAGE LOADER SETUP

Remove the import of either ImageLoader, and change the code to:

    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

            com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

    // END - UNIVERSAL IMAGE LOADER SETUP
Community
  • 1
  • 1
Leo
  • 1,433
  • 23
  • 40
1

You can remove imports and use com.android.volley.toolbox.ImageLoader and com.nostra13.universalimageloader.core.ImageLoader instead of ImageLoader in code.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45