Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley's NetworkImageView - setImageBitmap method doesn't work

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!

like image 486
dor506 Avatar asked Jan 21 '14 22:01

dor506


3 Answers

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

}
like image 138
Paramvir Singh Avatar answered Nov 17 '22 14:11

Paramvir Singh


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

}

like image 28
York Avatar answered Nov 17 '22 16:11

York


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));
    }
}
like image 2
Ratul Doley Avatar answered Nov 17 '22 16:11

Ratul Doley