BitmapFactory解码流到文件.或者如何处理位图而不杀死VM



在解码图像时,我正在努力解决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);

然后我需要把它保存回file:

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

如你所见,我的目标是在存储中减少图像。

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

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

有人知道怎么做吗?

尝试调用Bitmap。在加载下一个位图之前,在完成一个位图之后进行回收。

最新更新