加快下载时间

时间:2014-04-09 10:32:25

标签: android performance inputstream androidhttpclient

这段代码用于下载图片,有人可以告诉我如何优化此代码以减少每张图片的下载时间。

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

        }

2 个答案:

答案 0 :(得分:1)

当OrhancC1打败我的时候,我也忙着向毕加索提出建议,所以如果你最终使用它,就可以归功于他。

毕加索很棒!我们在其中一个项目中使用它,并设法使HTTP缓存正常工作。在我们的应用程序中,我们下载的图像不会经常更改,所以一旦第一次加载,从缓存中解析出任何后续加载,这比网络要快得多。

如果出于任何原因,这个问题的答案可能无关紧要:Speed Up download time

答案 1 :(得分:0)

就个人而言,我会使用像毕加索这样的东西。试一试

http://square.github.io/picasso/

相关问题