21

Im using volley library in my project.

I usually let the NetworkImageView download images using setImageUrl method:

networkImageView.setImageUrl(imageUrl, mImageLoader)

This works fine, But.. When I try to download the bitmap "manually" using ImageLoader's get method, and then set the bitmap by myself, it doesn't work:

mImageLoader.get(imageUrl,new ImageLoader.ImageListener()
    {
        @Override
        public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b)
        {
            if (imageContainer.getBitmap() != null)
            {
                networkImageView.setImageBitmap(imageContainer.getBitmap());
            }
        }

        @Override
        public void onErrorResponse(VolleyError volleyError)
        {

        }
    });

networkImageView.setImageBitmap(imageContainer.getBitmap()) line does nothing.

How could it be? Thanks in advance!

dor506
  • 5,246
  • 9
  • 44
  • 79
  • 1
    If you are going to load the image manually, then you can use a simple ImageView and set the bitmap your self. – Lefteris Jan 21 '14 at 23:24

6 Answers6

36

This version of NetworkImageView fixes this issue.

public class CustomNetworkImageView extends NetworkImageView {

    private Bitmap  mLocalBitmap;

    private boolean mShowLocal;

    public void setLocalImageBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            mShowLocal = true;
        }
        this.mLocalBitmap = bitmap;
        requestLayout();
    }

    @Override
    public void setImageUrl(String url, ImageLoader imageLoader) {
        mShowLocal = false;
        super.setImageUrl(url, imageLoader);
    }

    public CustomNetworkImageView(Context context) {
        this(context, null);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

    super.onLayout(changed, left, top, right, bottom);
    if (mShowLocal) {
            setImageBitmap(mLocalBitmap);
        }
    }

}
BananaNeil
  • 10,322
  • 7
  • 46
  • 66
Paramvir Singh
  • 2,354
  • 1
  • 20
  • 20
  • Also worked for me. I wonder why NetworkImageView has these problems!! thanks anyway! +1!! – voghDev Nov 24 '14 at 23:12
  • has problems. Though it worked, in a recyclerview where each row has a networkimageview, it wrongly shows the image – Winster Aug 10 '16 at 09:22
  • Similarly, we can add a method in order to enable ImageView's setImageUri() method to work. As follows: public void setLocalImageUri(Uri uri) { if (uri != null) { mShowLocalUri = true; } this.uri = uri; requestLayout(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if(mShowLocalUri) { setImageURI(uri); } } – Perry Apr 19 '17 at 08:34
3

You can achieve that by simply adding several lines of code in the source code of NetWorkImageView(I suppose you have the right to edit the source code, if you can't, you can just extends NetWorkImageView, it is pretty easy).

public class NetworkImageView extends ImageView {
    private Bitmap bitmap;
    public void setLocalImageBitmap(Bitmap bitmap){
        this.bitmap=bitmap;
    }
    /**The volley verison of NetworkImageView has This method, you just need to add
    a new condition, which is else if(bitmap!=null).
    **/
    private void setDefaultImageOrNull() {
    if(mDefaultImageId != 0) {
        setImageResource(mDefaultImageId);
    }
    else if(bitmap!=null){
        setImageBitmap(bitmap);
    }
    else {
        setImageBitmap(null);
    }
}

}

York
  • 503
  • 3
  • 23
2

The accepted answer did not work for me... The following code works:

    public class CustomNetworkImageView extends NetworkImageView {
    Context mContext;
    public CustomNetworkImageView(Context context) {
        super(context);
        mContext = context;
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        if (bm == null) return;
        setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
    }
}
Ratul Doley
  • 499
  • 1
  • 6
  • 12
0

As Lefteris mentioned, just use a normal ImageView if you are loading with the listener paradigm. This worked for me.

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
0

You can also set your local bitmap through the image loader and not touch NetworkImageView

 imageSaver = new ImageLoader(VolleyWebServiceManager.getInstance().getRequestQueue(), new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(50);

        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
            FileManager.getInstance().saveImage(bitmap, FileManager.getInstance().getLastPathComponent(url), true, false);
        }

        public Bitmap getBitmap(String url) {
            // Puts the bitmap from the file system into the cache
            if (mCache.get(url) == null && FileManager.getInstance().getLocalImage(FileManager.getInstance().getLastPathComponent(url)) != null) {
                putBitmap(url, FileManager.getInstance().getLocalImage(FileManager.getInstance().getLastPathComponent(url)));
            }
            return mCache.get(url);
        }
    });
Apetroaei Andrei
  • 426
  • 4
  • 12
0

Get NetworkImageView url it Will Use full.

private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;


private CustomVolleyRequestQueue(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new CustomVolleyRequestQueue(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        mRequestQueue = new RequestQueue(cache, network);
        // Don't forget to start the volley request queue
        mRequestQueue.start();
    }
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}
Satheeshkumar Somu
  • 496
  • 1
  • 7
  • 14