适用于大映像的Android VM堆策略 [英] Android VM heap strategies for big images

查看:65
本文介绍了适用于大映像的Android VM堆策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理较大的图像,大约5MP,有时甚至更多.我需要内存中的图像的两个副本才能进行操作. 现在,加载的图像会占用大量内存,超过了默认的Android堆(分别为16MB和24MB)所提供的内存,这会导致以下错误:

I am manipulating relative large images, about 5MP and sometimes even more. I need two copies of the images in memory for manipulation. Now, the loaded images consume a lot of memory, more than available by the default Android heap which is 16MB respectively 24MB which results in the following error:

11-20 18:02:28.984:E/AndroidRuntime(7334): java.lang.OutOfMemoryError:位图大小超出VM预算

11-20 18:02:28.984: E/AndroidRuntime(7334): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

我需要全分辨率,因此在加载图像时缩小图像没有帮助. 解决这个问题的最佳解决方案是什么?是否有内置方法可以动态地仅从存储中加载位图的一部分?有人可以给我一些提示,例如如何克服内存问题.通过使用特定的缓存策略?

I need full resolution, thus downscaling while loading the images does not help. What's the best solution to get over this problem? Are there built-in methods to dynamically load only chunks of bitmaps from storage? And can someone give me some hints how I can overcome the memory problem, e.g. by using specific caching strategies?

此致

推荐答案

您可以在ndk中分配更多的内存.您必须编写本机代码来处理图像,或者必须想出一种方法来以本机分配图像内存,然后将其传递回Java.

You can allocate more memory in the ndk. You'd have to write native code to manipulate the images, or you'd have to figure out a way to allocate the image memory in native, then pass it back to Java.

位图/画布使用和NDK

另一个选择可能是将单个图像加载到内存中,然后将其分解为多个块进行处理.将那些块保存到文件系统中.因此,假设您有2张大图片.您加载第一个图像,将其分为4个部分,保存它们,然后加载第二个图像,将其分为4个部分,保存它们,然后为每个图像加载第1部分,然后做您的事情.这意味着您知道每个图像都不大于堆最大值,并且您需要做的(基本上)是像素级的,并且不需要访问周围的像素数据(如果您遇到麻烦,则会在边缘遇到麻烦需要邻居像素信息.

Another option might be to load a single image into memory, and break it up into chunks for processing. Save those chunks out to the file system. So, say you 2 large images. You load the first image, break it into 4 parts, save them, load the second, break it into 4 parts, save those, then load part #1 for each image, and do your thing. That implies you know that neither individual image is larger than the heap max, and that what you need to do is (basically) pixel level and doesn't need access to surrounding pixel data (you'll run into trouble at the edges if you need neighbor pixel info).

如果没有下采样,分割或NDK,我不知道如何将更多图像数据存储到内存中.也许降低颜色信息.我们在产品中做到这一点.将每个像素表示为16位,而不是24或32位.我们的产品是实用的,而不是漂亮的",因此颜色信息的丢失不是什么大问题.

Without downsampling, splitting, or ndk, I don't know how you'd get more image data into memory. Perhaps lowering the color info. We do this in a product. Represent each pixel as 16 bits rather than 24 or 32. Our product is functional rather than "pretty", so the loss of color info wasn't a big deal.

这篇关于适用于大映像的Android VM堆策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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