Volley's NetworkImageView - setImageBitmap method doesn't work
Asked Answered
L

6

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!

Laky answered 21/1, 2014 at 22:12 Comment(1)
If you are going to load the image manually, then you can use a simple ImageView and set the bitmap your self.Pornocracy
A
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);
        }
    }

}
Acetophenetidin answered 19/3, 2014 at 8:56 Comment(3)
Also worked for me. I wonder why NetworkImageView has these problems!! thanks anyway! +1!!Kordofan
has problems. Though it worked, in a recyclerview where each row has a networkimageview, it wrongly shows the imageLysozyme
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); } }Chaddie
R
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);
    }
}

}

Runway answered 14/3, 2014 at 3:33 Comment(0)
T
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));
    }
}
Tare answered 9/3, 2017 at 7:14 Comment(0)
T
0

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

Talbot answered 25/3, 2014 at 18:16 Comment(0)
Y
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);
        }
    });
Yardmaster answered 27/11, 2014 at 18:15 Comment(0)
G
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;
}
Gyrostatic answered 18/12, 2015 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.