处理大型位图 [英] Handling large Bitmaps

查看:14
本文介绍了处理大型位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆图片网址.我必须下载这些图像并将它们一一显示在我的应用程序中.我正在使用 SoftReferences 和 Sdcard 将图像保存在集合中,以避免重新获取并改善用户体验.

I have a bunch of image URLs. I have to download these images and display them in my application one-by-one. I am saving the images in a Collection using SoftReferences and also on Sdcard to avoid refetches and improve user experience.

问题是我对位图的大小一无所知.事实证明,当我使用 BitmapFactory.decodeStream(InputStream) 方法时,偶尔会出现 OutOfMemoryExceptions.因此,我选择使用 BitmapFactory Options(样本大小=2)对图像进行下采样.这提供了更好的输出:没有 OOM,但这会影响较小图像的质量.

The problem is I dont know anything about the size of the bitmaps. And as it turns out, I am getting OutOfMemoryExceptions sporadically, when I am using BitmapFactory.decodeStream(InputStream) method. So, I chose to downsample the images using BitmapFactory Options(sample size=2). This gave a better output: no OOMs, but this affects the quality of smaller images.

我应该如何处理这种情况?有没有办法选择性地仅对高分辨率图像进行下采样?

How should I handle such cases? Is there a way to selectively downsample only high resolution images?

推荐答案

BitmapFactory.Options 类(我忽略的一个)中有一个选项名为 inJustDecodeBounds,javadoc of内容如下:

There is an option in BitmapFactory.Options class (one I overlooked) named inJustDecodeBounds, javadoc of which reads:

如果设置为true,解码器将返回 null(无位图),但是out... 字段仍将被设置,允许调用者查询位图而无需分配其像素的内存.

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

我用它来找出位图的实际大小,然后选择使用 inSampleSize 选项对其进行下采样.这至少可以避免在解码文件时出现任何 OOM 错误.

I used it to find out the actual size of the Bitmap and then chose to down sample it using inSampleSize option. This at least avoids any OOM errors while decoding the file.

参考:
1.
2. I解码前获取位图信息

这篇关于处理大型位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆