Android:通过http加载多个位图/拇指的最快方法是什么?

发布于 2024-10-07 13:01:10 字数 2090 浏览 2 评论 0原文

我需要知道:连续下载多个拇指(比如说 10 个图像)的最快方法是什么。看起来内置浏览器以一种非常聪明的方式做到了这一点。

我 Google 了一下,发现大多数开发人员都使用 HttpUrlConnection 类来下载 jpeg,以将其显示在 ImageView、ListAdapter 等中。

一些开发人员通过使用 DefaultHttpClient 类来实现下载,因为它对超时等有更好的支持。事实上Google 建议使用 Apache Http 客户端,而不是本机 Java HttpUrlConnection。也就是说,提到的两种策略都工作得很好,但与我的 HTC 上的内置浏览器下载缩略图的方式相比,速度非常慢。与尝试使用我自己的代码下载相同的位图相比,内置浏览器下载图像的速度似乎快了 5 倍到 10 倍。是的,我总是在单独的工作线程(而不是 GUI 线程)上执行下载/http 工作。

有人知道内置浏览器如何下载拇指,或者至少知道从服务器下载多个图像的最快方法是什么?

我尝试过使用此代码:

DefaultHttpClient client = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT);      
client.setParams(httpParameters);
client.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(HttpResponse response, HttpContext context)
    {
        return 5000;
    }
});

现在,我使用此连接来在 for 循环中获取多个位图/拇指:

public static Bitmap downloadBitmap(String url, DefaultHttpClient client){

  HttpResponse response = null;
  HttpGet get = new HttpGet(url);
  try {
      response = client.execute(get);
      return BitmapFactory.decodeStream(response.getEntity().getContent());
  } 
  catch (ClientProtocolException e1) {
      e1.printStackTrace();
  }
  catch (IllegalStateException e){
      e.printStackTrace();
  } 
  catch (IOException e1) {
      e1.printStackTrace();
  }

  return null;
}

我创建了一个包含 10 个位图的 testpage.html,并使用内置浏览器加载它。浏览器在 1 秒内用所有拇指渲染页面。我确信它们不会缓存在浏览器中,因为我会在每次页面刷新时随机化这 10 张图像。

当我尝试制作一个下载并显示相同拇指的简单活动时,速度要慢得多。

我希望 Google Android 团队能够注意到这一点,并将其包含在下一次开发者大会上的视频演讲中。

他们应该为此定义最佳实践,因为似乎每个开发者都试图解决这个问题这个“下载位图”用例有自己的方式,而我们实际上都在尝试做同样的事情。

我还测试了使用相同的 DefaultHttpClient 对象通过使用不同的 url(HttpGet 对象)调用execute() 来获取多个图像,但它的速度与内置浏览器的速度仍然相去甚远。我看到我的请求已设置连接保持活动标志,但它似乎没有任何区别。

I need to know : What is the fastest way to download several thumbs in a row, lets say 10 images. It seems like the built-in browser does this in a very clever way.

I have Google'd and found that most developers are using the HttpUrlConnection class to download a jpeg to display it in a ImageView, inside an ListAdapter etc.

Some developers implements the download by using DefaultHttpClient class as it has better support for timeouts etc. In fact Google recommends to use the Apache Http client, and not the native Java HttpUrlConnection. That said, both startegies mentioned works fine, but thay are very slow compared to how the build in browser on my HTC desire download thumbnails. The built in browser seems to download images about 5x-10x faster then when try to download the same bitmaps with my own code. And yes, I always do the download/http work on a separate worker thread (not GUI thread).

Do anybody know how the built-in browser is downloading thumbs, or at least what is the fastest way to download several images from a server ?

I have tried using this code:

DefaultHttpClient client = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT);      
client.setParams(httpParameters);
client.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(HttpResponse response, HttpContext context)
    {
        return 5000;
    }
});

Now, i use this connection to fetch several bitmaps / thumbs in a for-loop:

public static Bitmap downloadBitmap(String url, DefaultHttpClient client){

  HttpResponse response = null;
  HttpGet get = new HttpGet(url);
  try {
      response = client.execute(get);
      return BitmapFactory.decodeStream(response.getEntity().getContent());
  } 
  catch (ClientProtocolException e1) {
      e1.printStackTrace();
  }
  catch (IllegalStateException e){
      e.printStackTrace();
  } 
  catch (IOException e1) {
      e1.printStackTrace();
  }

  return null;
}

I created a testpage.html with 10 bitmaps and loaded it with the built-in browser. The browser renders the page with all the thumbs within 1 seconds. And I'm sure they are not cached in the browser, as i randomize the 10 images for every page-refresh.

When i try to make a simple activity that downloads and displays the same thumbs, its a lot slower.

I hope the Google Android Team picks up this and includes it in one of their video speaks on the next developer conference.

They should define a best-practise for this, as it seems that every developer tries to solve this "download-bitmap" use-case their own way, when we in fact are all trying to do the same thing.

I have also tested using the same DefaultHttpClient object to fetch several images by calling execute() with diffrent urls (HttpGet objects), but its still far from the speed to the built-in browser. I see that my requests has sat the connection keep-alive flag, but it does not seem to make any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2024-10-14 13:01:10

内置浏览器比您的代码更快的原因有两个:

  1. 浏览器主要以本机代码实现(例如 WebKit)
  2. 同时执行多个下载(多线程)

There are two reasons why the built-in browser is faster as your code:

  1. The browser is mainly implemented in native code (e.g. WebKit)
  2. Multiple downloads are performed simultaneously (multiple threads)
謸气贵蔟 2024-10-14 13:01:10

我会使用线程一次下载多个图像。这应该有助于使您的代码与浏览器保持一致。

I'd use threads to download multiple images at once. This should help put your code on par with the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文