Search code examples
androidurlandroid-imageandroid-collapsingtoolbarlayoutandroid-appbarlayout

Load image via URL in collapsible app bar using ImageLoader


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

Solution

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