通用图像加载器和302重定向

时间:2013-03-05 02:55:51

标签: universal-image-loader

使用UIL 1.8.0版加载推特个人资料图片网址: http://api.twitter.com/1/users/profile_image/smashingmag.jpg?size=bigger

带有光盘和内存缓存。图像无法加载和存储光盘缓存文件中302重定向附带的html。图像永远不会成功加载或解码(我的SimpleImageLoadingListener的onLoadingFailed方法会被调用每个twitter个人资料图片网址)。任何人都可以用UIL加载一个简单的推特图片网址吗?

以下是该网址的缓存文件的内容:

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>

这是我的配置:

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

1 个答案:

答案 0 :(得分:3)

似乎HttpURLConnection无法自动处理从HTTP到HTTPS的重定向(link)。我将在下一个lib版本中修复它。

现在修复 - 扩展BaseImageDownloader并将其设置为配置:

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