2013-03-05 47 views
0

使用UIL版本1.8.0加载Twitter个人资料图像URL: http://api.twitter.com/1/users/profile_image/smashingmag.jpg?size=bigger通用图像装载机和302重定向

与光盘和存储器的高速缓存。图像无法加载和存储伴随302重定向到光盘缓存文件中的html。图像从未加载或解码成功(我的SimpleImageLoadingListener的onLoadingFailed方法被调用了每个Twitter配置文件图像url)。任何人都可以使用UIL加载一个简单的Twitter图片网址?

以下是一个URL我的缓存文件的内容:

猫的/ mnt/SD卡/ 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); 

回答

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

谢谢!这正是问题所在。而你的修复效果很好。 – 2013-03-05 18:22:22

+0

这是给conn.getResponseCode()== 500 – mohitum 2016-01-29 09:26:49