Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing protected images in universal image loader

I am using the universal image loader in an app that needs to fetch images an authorized source.

So far, I have extended the URLConnectionImageDownloader class with my own class, and overridden the method getStreamFromNetwork with my own implemetation that sets the authorization header in the URLConnection object as such:

public class authURLConnectionImageDownloader extends URLConnectionImageDownloader {

@Override
public InputStream getStreamFromNetwork(URI imageUri) throws IOException {

    String auth = Base64.encodeToString(("username" + ":"+"psswd").getBytes(), Base64.NO_WRAP);

    URLConnection conn = imageUri.toURL().openConnection();
    conn.setRequestProperty("Authorization", "Basic " + auth);

    conn.setConnectTimeout(DEFAULT_HTTP_CONNECT_TIMEOUT);
    conn.setReadTimeout(DEFAULT_HTTP_READ_TIMEOUT);

    return new FlushedInputStream(new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE));     
}

and for setting up my ImageLoader...

imageLoader = ImageLoader.getInstance();

ImageLoaderConfiguration config =  new ImageLoaderConfiguration.Builder(MainActivity.this)
        .imageDownloader(new authURLConnectionImageDownloader())
        .build();

imageLoader.init(config);

So far I have unable to get it to work. The image is not downloaded. But more importantly I have put a breakpoint in getStreamFromNetwork() and it is never hit? What am I doing wrong?

like image 951
FlashAsh80 Avatar asked Nov 27 '12 14:11

FlashAsh80


2 Answers

I implemented it this way:

 byte[] toEncrypt = (username + ":" + password).getBytes();
        String encryptedCredentials = Base64.encodeToString(toEncrypt, Base64.DEFAULT);
        Map<String, String> headers = new HashMap();
        headers.put("Authorization","Basic "+encryptedCredentials);

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            ...
            .extraForDownloader(headers)
            .build();

Create your own ImageDownloader:

public class AuthDownloader extends BaseImageDownloader {

    public AuthDownloader(Context context){
        super(context);
    }

    @Override
    protected HttpURLConnection createConnection(String url, Object extra) throws IOException {
        HttpURLConnection conn = super.createConnection(url, extra);
        Map<String, String> headers = (Map<String, String>) extra;
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                conn.setRequestProperty(header.getKey(), header.getValue());
            }
        }
        return conn;
    }
}

and set it to the configuration:

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .discCacheExtraOptions(600, 600, CompressFormat.PNG, 75, null)
            .imageDownloader(new AuthDownloader(getApplicationContext()))
            .build();

    ImageLoader.getInstance().init(config);
like image 165
Lukas Lechner Avatar answered Nov 09 '22 23:11

Lukas Lechner


I managed to get it working in the end...

I used the library .jar with source that comes with the example, and debugged it. I saw that it was never accessing my URLConnectionImageDownloader derived class and always using the parent.

So looked at my code, and saw I was setting up another imageloader within a previous activity that was using the default URLConnectionImageDownloader class.

Now I have created an application class and setup my ImageLoader once (as in the example app), and set the config to use my new authURLConnectionImageDownloader class. Now all my activies use this ImageLoader and it works.

like image 44
FlashAsh80 Avatar answered Nov 09 '22 23:11

FlashAsh80