内存不足错误 - 在地图中使用位图时,位图超出 Android 中的 VM 预算?

发布于 2024-11-23 20:21:49 字数 239 浏览 1 评论 0原文

我正在将高度和宽度等于设备屏幕的图像绘制为 Android 中的覆盖项目。但是,当图像数量超过时,应用程序会因内存不足错误而崩溃 - 位图超出 VM 预算。如果我尝试回收它,则会出现错误,因为画布尝试使用回收的位图。我需要的是拥有在地图上绘制大量图像的最佳方法。图像来自服务器,我还需要缓存图像。目前,对于缓存,我正在 LinkedHashMap 中执行此操作,但我想即使这也会产生问题,因为我存储可绘制对象。

有没有在地图上使用绘制大位图的示例?

I am plotting images of height and width equal to device screen as an overlay item in Android. But when the number of images exceeds the app crashes with Out Of Memory error - Bitmap exceeds VM budget. If I try to recycle it then the error comes as canvas trying to use recycled bitmap. What I need is to have the best way of plotting lots of images over map. The images comes from the server and I also need to cache the images. For caching currently I am doing that in LinkedHashMap but I guess even this will create problems as I am storing the drawable objects.

Is there any example of using plotting large bitmaps on maps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-11-30 20:21:51

您可能需要考虑使用 inSampleSize 属性BitmapFactory.Options 类。创建位图对象时,此属性将按 2 的幂(由您决定)重新缩放图像,这将节省内存。如果图片的分辨率大于屏幕的分辨率,这应该非常适合您,而不会降低图片的质量。

要将其用于从服务器下载的图像,您可以按如下方式使用它:

URL url = new URL(photoUrl);
URLConnection ucon = url.openConnection();

Options options = new Options();
options.inSampleSize = 2;

Bitmap bitmap = BitmapFactory.decodeStream(ucon.getInputStream(),null, options);

另一种方法是使用 SoftReference 对象包装 HashMap 中的每个位图对象,以便 VM 回收该位图对象。位图使用的内存,而不是因 OOM 错误而崩溃。缺点是您必须重新加载位图,并且就我个人而言,我认为虚拟机在回收内存时非常激进。它很快就能回收内存。

You might want to consider using the inSampleSize property of the BitmapFactory.Options class. This property will rescale your image by a power of 2 (which you decide) when the bitmap object is created, which will save memory. If the resolution of the picture is greater than the resolution of the screen, this should work perfectly well for you, without degrading the quality of the picture.

To use this for an image you are downloading from a server, you can use it as follows:

URL url = new URL(photoUrl);
URLConnection ucon = url.openConnection();

Options options = new Options();
options.inSampleSize = 2;

Bitmap bitmap = BitmapFactory.decodeStream(ucon.getInputStream(),null, options);

An alternative is to also wrap each bitmap object in your HashMap with a SoftReference object so that the VM will reclaim the memory used by bitmaps, rather than crashing with an OOM error. The downside is that you would have to reload the bitmap and personally, I feel that the VM is aggressive when reclaiming memory. It reclaims memory pretty quickly.

浪推晚风 2024-11-30 20:21:51

每个应用程序的内存量非常有限(通常为 16MB,但我见过低至 14MB,高至 32MB)。这是特定于每个设备上运行的固件的。可以继承应用程序类以提供对“LowMemory”函数的访问,该函数可以在您的应用程序使用几乎所有内存时向您发出警报。

屏幕大小的图像不应该有问题,但要确保它们的尺寸没有过大(尽可能小)。

我还建议检查 Eclipse 中的内存统计信息,以了解您的应用程序使用了多少内存(了解其增长情况等)。

这个错误是处理地图和 Android 时最烦人的错误之一,您会在 SO 上找到大量与此问题相关的其他帖子

Each application has very limited amounts of memory (often 16MB, but I've seen as low as 14MB, and as high as 32MB). This is specific to the firmware running on each device. The application class can be inherited to provide access to a "LowMemory" function, which can alert you when your application has used almost all of its memory.

You shouldn't have problems with images the size of the screen, but make sure they're not ridiculously over-sized (have them as small as possible).

I'd also recommend checking out the memory stat in Eclipse to see how much memory your application is using (to see how it grows etc).

This error is one of the most annoying errors when dealing with Maps and Android and you will find plenty of other posts related to this issue here on SO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文