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, a
Solution 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:
publicclassAuthDownloaderextendsBaseImageDownloader {
publicAuthDownloader(Context context){
super(context);
}
@OverrideprotectedHttpURLConnection 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);
Solution 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.
Post a Comment for "Accessing Protected Images In Universal Image Loader"