android – Volley's NetworkImageView – setImageBitmap method doesn't work-ThrowExceptions

Exception or error:

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!

How to solve:

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

}

###

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

}

###

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

###

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

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *