处理大位图 [英] Handling large Bitmaps

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

问题描述

我有一堆图像的URL。我要下载这些图片,并在我的应用程序一个接一个显示。我使用 SoftReferences ,也对SD卡,以避免重新提取,并改善用户体验保存图像的集合。

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.

现在的问题是我不知道的位图的大小事情。而事实证明,我越来越OutOfMemoryExceptions偶然,当我使用 BitmapFactory.de codeStream(InputStream中)方法。所以,我选择了下采样(样本量= 2)使用BitmapFactory选项的图像。这给了一个更好的输出:无奥姆斯,但这种影响较小的图像的质量

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 类的选项 inJustDe codeBounds ,javadoc的案文如下:

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

如果设置为true,去codeR会   返回空(没有位图),但   出...领域仍然被设置,   允许调用者查询   位图,而不必分配   内存为它的像素。

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。<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/1e9d19c3adf1a768/afef1c8341a63ad2?lnk=gst&q=samuh#afef1c8341a63ad2">Handling较大的位图
2. <一href="http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a/3cd07c6a5d4518d0?lnk=gst&q=inJustde$c$cBounds#3cd07c6a5d4518d0">How我是否能够得到位图信息之前,我去code

Reference:
1. Handling larger Bitmaps
2. How do I get Bitmap info before I decode

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

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