Search code examples
androidandroid-volleynetworkimageview

Setting image from resource in NetworkImageView - Android


I would like to set the image of NetworkImageView from a resource.

I am currently using a configuration in which if there is a picture associated with the content, the image is downloaded from the web. If there is no image, a predefined resource is used.

final NetworkImageView image = (NetworkImageView) dialog.findViewById(R.id.image_quiz);
if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
if (noImage)
    image.setDefaultImageResId(R.mipmap.splashh);
else {
    urlImage = content.getImg();
    image.setImageUrl(urlImage, imageLoader);
}

This snippet in contained in a modal and gets called several times.

If there is a sequence of content without an image, the resource is shown correctly.

If there is a sequence of content with images, the images are shown correctly.

THE PROBLEM

If the first content requires an image, the picture is downloaded from the web without any issue. If the second content does not require an image, the previous image remains and is shown instead of the resource R.mipmap.splashh

Is there any method like setImageResource ?


Solution

  • Looking at the code of NetworkImageView, I saw that it sets SetImageBitmap(null) to clean the currentImage.

    You can try to set it before calling setDefaultImageResId but you will let the NetworkImageView in a invalid state internally.

    I would recommend to use a more powerful library, like Square's Picasso or Google's Glide