1

我在解码图像时遇到 OOM 错误。这条消息对我正在处理的钱是正确的:BitmapFactory OOM 让我发疯

我想我知道什么可以解决我的问题,但不知道该怎么做。现在当我调整图像大小时 - 我得到这样的位图:

//Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            o2.inDither=false;          //Disable Dithering mode
            o2.inPurgeable=true;        //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            o2.inInputShareable=true;   //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            o2.inTempStorage=new byte[32 * 1024];
            Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, o2);

而且我需要将其保存回文件:

FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

如您所见 - 我的目标是减少存储中的图像。

现在 - 当我操作大位图时 - 我将大文件加载到内存中,这会增加堆大小,然后在下一次尝试时 - 我得到 OOM 异常,因为本机堆中没有足够的内存。

所以,我想从 BitmapFactory 获取流并以某种方式将其传递给 FileStream 进行存储。如果我想要大图像,那将消除 VM 堆问题。

有人知道怎么做吗?

4

1 回答 1

0

在完成一个位图之后但在加载下一个之前尝试调用Bitmap.recycle 。

于 2011-07-11T03:13:06.073 回答