Home Ask Login Register

Developers Planet

Your answer is one click away!

Sohail Zahid February 2016

Initialization of ImageLoaders on App Launch

How can i initialize image loader on app launch so i can use it everywhere in my application.I am trying to use volley library.

 if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());


NightSkyDev February 2016

I would create a singleton. Here's an example from a project I posted:

public class VolleySingleton {

    private static Context mContext;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static VolleySingleton ourInstance;
    public static VolleySingleton getInstance(Context context) {
        if (ourInstance == null) {
            ourInstance = new VolleySingleton(context);
        return ourInstance;

    private VolleySingleton(Context context) {
        mRequestQueue = Volley.newRequestQueue(context.getApplicationContext());
        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);

    public <T> void addToRequestQueue(Request<T> req) {

    public ImageLoader getImageLoader() {
        return mImageLoader;

Then you can get the image loader with this statement:


Igor Monochkov February 2016

Try this:

public class Application extends android.app.Application {

    public void onCreate() {

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)

Post Status

Asked in February 2016
Viewed 2,569 times
Voted 8
Answered 2 times


Leave an answer

Quote of the day: live life