Search code examples
androidimageviewandroid-volleyimage-loading

Volly imageLoader onResponse imageContainer width null


I'm trying to get image bitmap and redraw it in rounded image . but unfortunately i got NPE on imageContainer [the width is actually NULL].

here is what i have tried so far,

@Override
                public void onResponse(ImageContainer image, boolean arg1) {

                    RoundedDrawable rd=new RoundedDrawable(image.getBitmap());
                    holder.image.setImageDrawable(rd);

                }

Solution

  • It is not the width that is null, it is the bitmap.

    You probably want to see this: Volley image bitmap is null

    onResponse() can and will give you a null bitmap sometimes. In fact, onResponse() is called with a null bitmap once whenever the image is not found in the ImageLoader cache.
    This call with a null bitmap happens when the ImageLoader starts to fetch a remote image (not in cache).

    This is actually a good thing.

    It allows you to present the user with some kind of wait-while-image-is-loading image or progress bar.

    onResponse() will be called again later with a non null bitmap if everything goes right.

    So really, all you have to do is:

    public void onResponse(ImageContainer image, boolean arg1) {
        if(image.getBitmap() != null){
           RoundedDrawable rd = new RoundedDrawable(image.getBitmap());
           holder.image.setImageDrawable(rd);
        }
        else
           //set a waiting image or progress bar
    }