Android:下载图像和转换为位图时出现问题

发布于 2024-09-05 10:50:14 字数 1282 浏览 9 评论 0原文

我正在开发一个从 url 下载图像的应用程序。问题是只有一些图像被正确下载,而其他图像则没有。 首先,这是问题代码:

public Bitmap downloadImage(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = null;
     try {
         response = client.execute(new HttpGet(url));
     } catch (ClientProtocolException cpe) {
        Log.i(LOG_FILE, "client protocol exception");
         return null;
     } catch (IOException ioe) {
            Log.i(LOG_FILE, "IOE downloading image");
            return null;
     } catch (Exception e) {
            Log.i(LOG_FILE, "Other exception downloading image");
            return null;
     }

     // Convert images from stream to bitmap object
     try {
         Bitmap image = BitmapFactory.decodeStream(response.getEntity().getContent());
         if(image==null)
             Log.i(LOG_FILE, "image conversion failed");
         return image;
     } catch (Exception e) {
         Log.i(LOG_FILE, "Other exception while converting image");
         return null;
     }
}

所以我有一个方法,它将 url 作为字符串参数,然后下载图像,通过 BitmapFactory.decodeStream 方法将 HttpResponse 流转换为位图,然后返回它。问题是,当我使用较慢的网络连接(几乎总是 3G 而不是 Wi-Fi)时,一些图像会转换为空——不是全部,只是其中一些。使用 Wi-Fi 连接效果很好;所有图像均已下载并正确转换。

有谁知道为什么会发生这种情况?或者更好的是,我该如何解决这个问题?我该如何进行测试来确定问题?任何帮助都很棒;谢谢你!

I am working on an application that downloads images from a url. The problem is that only some images are being correctly downloaded and others are not.
First off, here is the problem code:

public Bitmap downloadImage(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = null;
     try {
         response = client.execute(new HttpGet(url));
     } catch (ClientProtocolException cpe) {
        Log.i(LOG_FILE, "client protocol exception");
         return null;
     } catch (IOException ioe) {
            Log.i(LOG_FILE, "IOE downloading image");
            return null;
     } catch (Exception e) {
            Log.i(LOG_FILE, "Other exception downloading image");
            return null;
     }

     // Convert images from stream to bitmap object
     try {
         Bitmap image = BitmapFactory.decodeStream(response.getEntity().getContent());
         if(image==null)
             Log.i(LOG_FILE, "image conversion failed");
         return image;
     } catch (Exception e) {
         Log.i(LOG_FILE, "Other exception while converting image");
         return null;
     }
}

So what I have is a method that takes the url as a string argument and then downloads the image, converts the HttpResponse stream to a bitmap by means of the BitmapFactory.decodeStream method, and returns it. The problem is that when I am on a slow network connection (almost always 3G rather than Wi-Fi) some images are converted to null--not all of them, only some of them. Using a Wi-Fi connection works perfectly; all the images are downloaded and converted properly.

Does anyone know why this is happening? Or better, how can I fix this? How would I even go about testing to determine the problem? Any help is awesome; thank you!

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

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

发布评论

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

评论(2

久隐师 2024-09-12 10:50:14

这是 JPEG 解码器的一个已知问题。有两种解决方案。要么使用 ByteInputStream 将整个图像下载到 byte[] 数组中,然后解码该数组(这就是我在 code.google.com/p/shelves。)另一个解决方案是创建一个包装器 InputStream,如下所示:

public class PatchInputStream extends FilterInputStream {

  public PatchInputStream(InputStream in) {
    super(in);
  }

  public long skip(long n) throws IOException {
    long m = 0L;
    while (m < n) {
      long _m = in.skip(n-m);
      if (_m == 0L) break;
      m += _m;
    }
    return m;
  }

}

This is a known issue with the JPEG decoder. There are two solutions. Either you download the entire image in a byte[] array using a ByteInputStream and then decode the array (this is what I do in code.google.com/p/shelves.) Another solution is to create a wrapper InputStream as shown below:

public class PatchInputStream extends FilterInputStream {

  public PatchInputStream(InputStream in) {
    super(in);
  }

  public long skip(long n) throws IOException {
    long m = 0L;
    while (m < n) {
      long _m = in.skip(n-m);
      if (_m == 0L) break;
      m += _m;
    }
    return m;
  }

}
短叹 2024-09-12 10:50:14

即使使用 WiFi 连接,某些位图(特别是 .BMP)也不会被解码。它只是一个有缺陷的解码器,无法处理延迟。如果您搜索 stackoverflow,您会发现一些其他解决方案,例如将 HTTP 流包装在缓冲的 http 实体中。这可行,但根据图像大小可能会占用大量内存。对于我们的商业产品,我们最终将 http 流下载到 sdcard,然后在下载的文件上使用 Bitmapfactory。它有点慢,但 100% 可靠。

Even with a WiFi connection some bitmaps (in particular .BMPs) will not be decoded. It's just a buggy decoder that can not deal with delays. If you search stackoverflow you will find some other solutions such as wrapping the HTTP stream in a buffered http entity. That works but depending on image size can take up a lot of memory. For our commerical product we ended up downloading the http stream to sdcard and then using Bitmapfactory on the dowloaded file. It's somewhat slower but 100% reliable.

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