这是一段用来下载图片的代码,有人能告诉我如何优化这段代码来减少每个图片的下载时间吗?
URL url;
HttpURLConnection connection = null;
InputStream input = null;
System.setProperty("http.keepAlive", "true");
try {
url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(HTTP_CONNECTION_TIMEOUT);
connection.setRequestProperty("Connection", "Keep-Alive");
input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
currentRequestInProgress.remove(urlString);
if (connection != null)
connection.disconnect();
if(input != null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}发布于 2014-04-09 18:59:17
当OrhancC1抢先一步推荐毕加索时,我也在忙着推荐毕加索,所以如果你最终使用了毕加索,请相信他。
毕加索太棒了!我们在我们的一个项目中使用了它,并设法使HTTP缓存正常工作。在我们的应用程序中,我们下载的图像不会频繁更改,因此一旦加载,任何后续加载都会从缓存中解析出来,这比网络快得多。
如果出于某种原因,这不是一个选项,那么这个问题的答案可能是相关的:Speed Up download time。
发布于 2014-04-09 18:40:35
就我个人而言,我只会用毕加索这样的东西。试一试
http://square.github.io/picasso/
https://stackoverflow.com/questions/22959871
复制相似问题