Android的位图处理 - 无泄漏,但仍然OOM? [英] Android bitmap processing - no leak but still OOM?

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

问题描述

我正在写一个相机应用,当我拍照,我收到一个byte [],德code它变成一个位图并将其保存为JPEG之前旋转。我用旋转本机库的照片,但是位仍然是来自一个byte []德codeD到内存中以便这(是,允许我保留1位图,而不是2)。所以基本上有1发生在我的code其中我需要大量的内存和OOM某些设备上,其中堆低,相机是愚蠢的,百万像素。任何建议如何在不失去的图像质量解决这个问题?

我不认为我想用 largeHeap =真正的
我应该忘记的旋转和刚刚成立EXIF?

另外我不是想'predict如此热衷,如果我将作为OOM数学不是加起来:<一href=\"http://stackoverflow.com/questions/30258478/android-outofmemory-when-gc-reports-free-memory\">Android内存不足时,GC报告的可用内存?


解决方案

  

任何建议如何解决这一问题而对失去的图像质量?


使用的android:largeHeap =真正的

或者使用一些其他的机库,可以让你交出字节[] 并执行旋转和保存到磁盘你,避免位图和庞大的东西在任何Java级别的处理。

或者,如果你的的minSdkVersion 19,你的逻辑剩下支持它,使用 inBitmap BitmapFactory.Options 来尝试重用一个已分配的位图对象,而不是分配一个新的。此选项适用于早期版本的Andr​​oid,但对于那些它必须是在分辨率方面完全匹配;上19+,在位图被重用只是必须足够大,以处理你正在尝试加载到它。


  

我不认为我想用largeHeap =真正的


这可能无法帮助(不是所有的设备会给你更大的堆),并有成本给用户更大的堆限制。话虽这么说,操纵大相机的图像是不困难更多的内存,无论是从本地code操作系统的Java堆或重新presents分配。


  

我应该忘掉旋转和刚刚成立EXIF?


这当然另一种可能性,虽然各种事情,如的ImageView ,似乎忽略EXIF头。


  

我不是想'predict如此热衷,如果我将作为OOM数学不是加起来


在某种程度上,这是因为Dalvik的没有压实/移动GC,艺术只有一个,而你的应用程序是不是在前台。 的OutOfMemoryError 意味着有记忆您试图分配什么没有单一的连续空闲块。

I'm writing a camera app and when I take a picture, I receive a byte [], decode it into a Bitmap and rotate it before saving it to JPEG. I'm rotating the photo using a native library, however the bitmap is still decoded from a byte[] into memory for this (still, allows me to keep 1 Bitmap instead of 2). So essentially there's 1 place in my code where I require a lot of memory and OOM on some devices where heap is low and cameras are stupid-megapixels. Any suggestions how to fix this without on loosing image quality?

I don't think I want to use largeHeap="true" Should I forget about rotation and just set EXIF?

Also I'm not so keen on trying to 'predict' if I will OOM as the math's not adding up: Android OutOfMemory when GC reports free memory?

解决方案

Any suggestions how to fix this without on loosing image quality?

Use android:largeHeap="true".

Or, use some other native library that allows you to hand over the byte[] and does the rotation and saving to disk for you, to avoid the Bitmap and any Java-level processing of the huge thing.

Or, if your minSdkVersion is 19, and the rest of your logic supports it, use inBitmap on BitmapFactory.Options to try to reuse an already-allocated Bitmap object rather than allocate a fresh one. This option is available on earlier versions of Android, but for those it has to be an exact match in terms of resolution; on 19+, the Bitmap to be reused just has to be big enough to handle what you are trying to load into it.

I don't think I want to use largeHeap="true"

It may not help (not all devices will give you a larger heap), and there are costs to the user for the larger heap limit. That being said, manipulating big camera images is difficult without more memory, whether that is in the Java heap or represents allocations from the OS from native code.

Should I forget about rotation and just set EXIF?

That's certainly another possibility, though various things, like ImageView, seem to ignore EXIF headers.

I'm not so keen on trying to 'predict' if I will OOM as the math's not adding up

In part, that's because Dalvik does not have a compacting/moving GC, and ART only has one while your app is not in the foreground. OutOfMemoryError means that there is no single contiguous free block of memory for what you are trying to allocate.

这篇关于Android的位图处理 - 无泄漏,但仍然OOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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