-1

I am trying to load image into collapsible app bar via ImageLoader class. When I am using the same code on other activities/fragments/recyclerview, its working perfectly but it is failing in collapsible app bar.

Here is the code that I am using:

                ImageLoader myLoader=new ImageLoader(getApplicationContext());
                myLoader.DisplayImage(flag,ivFlag);

I think might be I am not giving the correct context.

I tried using loading image manually through this code and its working perfectly:

ivFlag.setImageDrawable(getResources().getDrawable(R.drawable.india));
halfer
  • 19,824
  • 17
  • 99
  • 186
Adhish Lal
  • 149
  • 1
  • 3
  • 8

2 Answers2

0

I used NetworkImageView instead of simple ImageView and it worked like charm. This is how I did:

private ImageLoader mImageLoader;    
NetworkImageView proficPic,ivFlag;

and this is how I used it in my onCreate() method.

mImageLoader = VolleySingleton.getInstance().getImageLoader();

                proficPic.setImageUrl(imageURL, mImageLoader);
                ivFlag.setImageUrl(flag, mImageLoader);

And here is my VolleySingleton.java class

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;


import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.truelancer.app.ProfileDetail;


public class VolleySingleton {
    private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;


    private VolleySingleton(){
        mRequestQueue = Volley.newRequestQueue(ProfileDetail.getAppContext());
        mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
    }


    public static VolleySingleton getInstance(){
        if(mInstance == null){
            mInstance = new VolleySingleton();
        }
        return mInstance;
    }


    public RequestQueue getRequestQueue(){
        return this.mRequestQueue;
    }

    public ImageLoader getImageLoader(){
        return this.mImageLoader;
    }


}
Adhish Lal
  • 149
  • 1
  • 3
  • 8
0

A, perhaps inefficient, way is as follows:

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.loadImage(IMAGE_URI, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            // Sets the background drawable in callback
            BitmapDrawable drawable = new BitmapDrawable(loadedImage);
            collapsingToolbarLayout.setBackground(drawable);
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }
    });