volley network image view and request headers

2.4k views Asked by At

I have network image view which should get populated when I get the response back from the server in JSON format.

I get back a URL in json and I go to URL and show the image in my list view in android. However, this works fine if I use any image which is publicly hosted on internet.

In my case server is on IBM Domino and everything is in the domino database. The URL won't open unless i have user credentials to do so.

For general request responses using volley, I set a session ID in request headers, telling the domino server that i have authenticated myself already. however, in case of network Image view, I am unable to proceed as i do not know how to authenticate myself with the server.

The images do not show in the list view and I see following in my logcat

SkImageDecoder::Factory returned null 

I tried to open the same URL in browser, it asks for my user ID password and if I login, I am able to download the jpg file on my system.

Is there a way to set my http request headers for network image view ? How shall I proceed in this case ? Please help

Thanks

2

There are 2 answers

11
random On BEST ANSWER

NetworkImageView uses ImageLoader which internally makes ImageRequests using makeImageRequest

ImageRequest extends from Request. Request class has a getHeaders() method which allows to set request headers.

So if you can override makeImageRequest method of ImageLoader and then override getHeaders() for ImageRequest inside it, you should be able to set your request headers for NetworkImageView

@Override
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight, final String cacheKey) {
    //return super.makeImageRequest(requestUrl, maxWidth, maxHeight, cacheKey);

    return new ImageRequest(requestUrl, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            onGetImageSuccess(cacheKey, response);
        }
    }, maxWidth, maxHeight,
            Bitmap.Config.RGB_565, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            onGetImageError(cacheKey, error);
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("Authorization", YOUR_TOKEN);
            return params;
        }
    };
}
0
Zain On

I tried using the above ImageLoader, but couldn't seem to override the makeImageRequest method.

When looking at the Volley implementation I couldn't see the same method signature, so I used the same approach by @random - here's my CustomImageLoader.

public class CustomImageLoader extends ImageLoader {

private Context mContext;

/**
 * Constructs a new ImageLoader.
 *
 * @param queue      The RequestQueue to use for making image requests.
 * @param imageCache The cache to use as an L1 cache.
 */
public CustomImageLoader(Context aContext, RequestQueue queue, ImageCache imageCache) {
    super(queue, imageCache);
    mContext = aContext;
}


@Override
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight,
                                           ImageView.ScaleType scaleType, final String cacheKey,
                                           final ImageRequest.Transformation transformation) {

    return new ImageRequest(requestUrl, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            onGetImageSuccess(cacheKey, response);
        }
    }, maxWidth, maxHeight, scaleType, Bitmap.Config.RGB_565, transformation, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            onGetImageError(cacheKey, error);
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return getHeaders(mContext); //*YOUR* method for getting headers

        }
    };

}
  }