Android 图像捕捉时内存不足
我有一个拍摄照片的活动(具有全部可能的分辨率,因此相当大),应用程序有机会分析它们。一次仅处理一张照片。问题是我在拍了 4 - 5 张照片后遇到了“内存不足”的情况。我懂了
dalvikvm-heap 5070745 字节分配内存不足
(字节大小各不相同),然后是
Camera-JNI 无法为 JPEG 数据分配字节数组
我的应用程序没有崩溃,但在我看来,相机从此时开始无法传输图像。我关注应用程序中的内存使用情况,但在这里似乎外部某处存在内存泄漏,我问我如何应对这个问题。对此存在任何解决方案吗?
I have an Activity which takes photos (with full possible resolution, so quite large), the application have then the chance to analyze them. Only one photo is handled at a time. The problem is that I run in to a "Out of memory" after 4 - 5 photos. I see
dalvikvm-heap Out of memory on a 5070745-byte allocation
(the byte size varies) followed by
Camera-JNI Couldn't allocate byte array for JPEG data
My application does not crash but as it appears to me the Camera simply becomes unable to deliver the images from this point on. I pay attention to memory usage in my application but here it seems that there is a memory leak somewhere outside and I'm asking me how can I cope with this. Any solution approaches existing for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能不完全是您想要做的,但为了在网格视图上显示多个大图片(4-6 MB),我发现此代码可以创建质量较低的较小位图,以避免出现问题内存情况:
选项是重要的部分,通过改变设置,当我开始使用 inSampleSize 时,我设法将内存从崩溃的地方减少到 28MB 左右。当 inPurgeable 和 inInputShareable 设置为 true 时,它会进一步下降。我的活动仍然迟缓,但已经好多了。
对于您的应用程序,如果它可以分析位图,上面的代码可能会减少内存使用量以使其正常工作。我还是 Android 新手,所以这可能根本不起作用..;-)。
问候,
凯文
This may not be exactly what you are trying to do, but in order to display multiple large pictures (4-6 MB) on a grid view, I found this code to create a smaller bitmap with lower quality, to avoid out-of-memory situations:
The options are the important part, and by varying the settings, I managed to take memory down from where it would crash, to around 28MB when I started using the inSampleSize. It further went down with the inPurgeable and inInputShareable settings set to true. My activity is still sluggish, but it's much better.
For your application, if it can analyze a bitmap, the above code may shrink down the memory usage enough to make it work. I'm still new to Android, so it's possible this may not work at all.. ;-).
Regards,
Kevin
由于您在 4-5 张图片后内存不足,因此您可能不会调用 yourBitmap.recycle();保存到SD卡后?
另外,在 onPictureTaken() 方法中,如果不需要 alpha 通道,您可以使用 Bitmap.Config.RGB_565 而不是 ARGB(默认)将图片中的 tempData 保存到位图中。
Since you run out of memory after 4-5 pictures you probably arent calling yourBitmap.recycle(); after it has been saved to the SD-card?
Also in the onPictureTaken() method you could save the tempData from the picture into a bitmap using the Bitmap.Config.RGB_565 instead of ARGB(default) if you don't need the alpha channel.