Using UIL version 1.8.0 to load a twitter profile image url: http://api.twitter.com/1/users/profile_image/smashingmag.jpg?size=bigger
with disc and memory cache. The images are failing to load and storing the html that comes along with the 302 redirect in the disc cache file. The images never load or decode successfully (the onLoadingFailed method of my SimpleImageLoadingListener gets called for every twitter profile image url). Can anyone load a simple twitter image url with UIL?
Here is the content of my cache file for that url:
cat /mnt/sdcard/MyCache/CacheDir/1183818163
<html><body>You are being <a href="https://si0.twimg.com/profile_images/3056708597/6438618743e2b2d7d663fd43412bdae8_bigger.png">redirected</a>.</body></html>
Here is my configuration:
File cacheDir = StorageUtils.getOwnCacheDirectory(FrequencyApplication.getContext(), "MyCache/CacheDir");
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(FrequencyApplication.getContext())
.memoryCacheExtraOptions(480, 800)
.threadPoolSize(20)
.threadPriority(Thread.MIN_PRIORITY)
.offOutOfMemoryHandling()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.discCache(new TotalSizeLimitedDiscCache(cacheDir, 30 * 1024 * 1024))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(MyApplication.getContext(), 20 * 1000, 30 * 1000))
.tasksProcessingOrder(QueueProcessingType.FIFO)
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
It seems HttpURLConnection
can't handle redirect from HTTP to HTTPS automatically (link). I'll fix it in next lib version.
Fix for now - extend BaseImageDownloader
and set it into configuration:
public class MyImageDownloader implements BaseImageDownloader {
@Override
protected InputStream getStreamFromNetwork(URI imageUri, Object extra) throws IOException {
HttpURLConnection conn = (HttpURLConnection) imageUri.toURL().openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.connect();
while (conn.getResponseCode() == 302) { // >=300 && < 400
String redirectUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection) new URL(redirectUrl).openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.connect();
}
return new FlushedInputStream(conn.getInputStream(), BUFFER_SIZE);
}
}