Accessing protected images in universal image loader

五迷三道 提交于 2019-11-29 21:40:51

问题


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?


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/13586183/accessing-protected-images-in-universal-image-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!