Search code examples
androidjsonlotus-dominoandroid-volley

volley network image view and request headers


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


Solution

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