Skip to content Skip to sidebar Skip to footer

How To Download An Image By Using Volley?

I am Trying to download an Image into an ImageView by using the Volley Library. I inject the response of the Volley Library into the ImageView, but I am not getting the desired res

Solution 1:

You are using the wrong type of request. There is an ImageRequest or ImageLoader.

Please refer to an example snippet in the docs or the example below:

ImageView mImageView;
String url = "http://i.imgur.com/7spzG.png";
mImageView = (ImageView) findViewById(R.id.myImage);
...

// Retrieves an image specified by the URL, displays it in the UI.ImageRequest request = newImageRequest(url,
    newResponse.Listener<Bitmap>() {
        @OverridepublicvoidonResponse(Bitmap bitmap) {
            mImageView.setImageBitmap(bitmap);
        }
    }, 0, 0, null,
    newResponse.ErrorListener() {
        publicvoidonErrorResponse(VolleyError error) {
            mImageView.setImageResource(R.drawable.image_load_error);
        }
    });
// Access the RequestQueue through your singleton class.MySingleton.getInstance(this).addToRequestQueue(request);

Or you can use NetworkImageView, also part of Volley.

However, be advised that Google has deprecated their own Volley by phasing out Apache, but there is a temp workaround: How to use the legacy Apache HTTP client on Android Marshmallow?

Furthermore, now with P+, if your app uses Google Maps SDK, you will additionally need to add a uses-library/false attribute

But like most people will suggest, if you can, use Picasso http://square.github.io/picasso/ or something newer.

Solution 2:

Use this constructor version:

new ImageRequest(url, myResponseListener, maxWidth,
                 maxHeight, scaleType, Config.RGB_565, myErrorListener);

This constructor isn't deprecated in Volley ImageRequest.

Post a Comment for "How To Download An Image By Using Volley?"