我的应用程序中有一个 ListView,ListView 的项目将显示图像。所以我需要异步加载器从 Http 加载图像。
现在我用谷歌的Bitmapfun,但你知道,它很复杂,至少我是这么认为的。
所以我想重新编码,遇到一个问题。
如何从 Http 下载相同的 Url 图片并且不重复 httpRequest 和异步执行?
因为在ListView的item中,可能显示的是同一张图片。在Bitmapfun,google“同步”了进程,所以下载图片无法并发。</p>
帮我!想一想T_T
.........对不起,英语不好。
一个 ListView,例如,有 5 个项目。每个项目都有一个 ImageView 来显示图像。我使用 AsyncTask 发出 Http 请求,下载它,放入缓存,并显示它。
可能是五个图像是
"hxxp://xxx.com/1.jpg",
"hxxp://xxx.com/2.jpg",
"hxxp://xxx.com/3.jpg",
"hxxp://xxx.com/1.jpg",
"hxxp://xxx.com/1.jpg"
所以这三个Http请求是一样的。但是 AsyncTask 是同时工作的。所以另外两个Http请求被重复了。
我要解决的问题是“如何为 AsyncTask 下载图像但如果相同的 Url 只下载一次?”.....
编辑: 使用惰性列表:
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null){
Log.d("ImageLoader", " From Cache" + url);
return b;
}
//from web
try {
Log.d("ImageLoader", " Begin Downloading" + url);
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
初始化 ListView 和日志是:
04-09 10:44:31.900: D/ImageLoader(8578): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:44 :31.910: D/ImageLoader(8578): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:44:31.910: D/ImageLoader (8578): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:44:31.910: D/ImageLoader(8578): 开始下载hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:44:31.930: D/ImageLoader(8578): 开始下载 hxxps://lh6.谷歌用户内容。com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:44:33.090:I/MemoryCache(8578):缓存大小=44032 长度=1 04-09 10:44:33.090: D/ImageLoader(8578):开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg 04-09 10:44:33.140: I/MemoryCache(8578): 缓存大小=44032 长度=1 04-09 10:44:33.140: D/ImageLoader(8578): 开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs /s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpgcom/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg 04-09 10:44:33.140: I/MemoryCache(8578): 缓存大小=44032 长度=1 04 10:44:33.140: D/ImageLoader(8578): 开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpgcom/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg 04-09 10:44:33.140: I/MemoryCache(8578): 缓存大小=44032 长度=1 04 10:44:33.140: D/ImageLoader(8578): 开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg
使用https://github.com/nostra13/Android-Universal-Image-Loader
if (bmp != null && !bmp.isRecycled()) {
Log.d("ImageLoader", " From Cache " + uri);
if (configuration.loggingEnabled)
L.i(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);
if (options.shouldPostProcess()) {
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageView, targetSize, options, listener,
engine.getLockForUri(uri));
ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, options.getHandler());
engine.submit(displayTask);
} else {
options.getDisplayer().display(bmp, imageView);
listener.onLoadingComplete(uri, imageView, bmp);
}
} else {
Log.d("ImageLoader", " Begin Downloading " + uri);
if (options.shouldShowStubImage()) {
imageView.setImageResource(options.getStubImage());
} else {
if (options.isResetViewBeforeLoading()) {
imageView.setImageBitmap(null);
}
}
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageView, targetSize, options, listener,
engine.getLockForUri(uri));
LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo, options.getHandler());
engine.submit(displayTask);
}
初始化 ListView 和日志是:
04-09 10:23:24.590: D/ImageLoader(7336): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:23 :24.600: D/ImageLoader(7336): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:23:24.600: D/ImageLoader (7336): 开始下载 hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:23:24.610: D/ImageLoader(7336): 开始下载hxxps://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:23:24.610: D/ImageLoader(7336): 开始下载 hxxps://lh6.谷歌用户内容。com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg 04-09 10:23:24.610:D/ImageLoader(7336):开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0I /AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg 04-09 10:23:24.610:D/ImageLoader(7336):开始下载 hxxps://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0Ik /AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpgcom/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpgcom/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg
所以 httpRequest 是 Repeat....