Adding Authentication Headers to NetworkImageView in Volley
Asked Answered
M

2

7

I use Volley and NetworkImageView. This has been working great for several projects. However I need to send auth token in headers for my image request right now. What is the best way to do this? In my normal requests I override getHeaders() and put my token in that way. But ImageLoader which is what NetworkImageView uses generates a generic Request, making it hard to add headers...

Miscible answered 21/3, 2014 at 20:4 Comment(0)
M
0

Haven't found an easy way of doing this. I have forked Volley to accomplish what I needed. Feel free to use if you need it.

https://github.com/leofirespotter/ubervolley

Also added in here is making a rounded NetworkImageView copied from (http://solutionforandroid.blogspot.com/2013/12/rounded-networkimageview-using-volley.html)

Miscible answered 24/3, 2014 at 22:10 Comment(0)
T
6

I have come up with a solution. In order to use NetworkImageView you need to create an image loader. At this point of the code, just overide the makeImageRequest() method of ImageLoader, and inside makeImageRequest(), overide the getHeaders() method of 'ImageRequest' :

imageLoader = new ImageLoader(requestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            }) {
        @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;
                }
            };
        }
    };

This is technically one line of code :) I have tested this and it works for me.

Throaty answered 16/12, 2014 at 22:47 Comment(1)
onGetImagesSuccess() has private access in ImageLoader.Goetz
M
0

Haven't found an easy way of doing this. I have forked Volley to accomplish what I needed. Feel free to use if you need it.

https://github.com/leofirespotter/ubervolley

Also added in here is making a rounded NetworkImageView copied from (http://solutionforandroid.blogspot.com/2013/12/rounded-networkimageview-using-volley.html)

Miscible answered 24/3, 2014 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.