位图大小超过Android上的VM预算错误 [英] bitmap size exceeds VM budget Error on Android

查看:203
本文介绍了位图大小超过Android上的VM预算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载从资源文件夹100张图像数组对象。这些照片是非常小(PNG〜20K),所用这个code做它的IM,以及prevent内存泄漏和放大器;优化的性能:

I'm loading 100 images from Asset folder to an array object. The pictures are quite small (png ~20k each), and im using this code do to it, and to prevent memory leak & optimized performances:

在一个循环

// create resized bitmap from asset resource
        InputStream istr = assetManager.open(pics[i]);
        Bitmap b = BitmapFactory.decodeStream(istr);
        b = Bitmap.createScaledBitmap(b, 240, 240, true);

在那里图片[i]为它坐落在我的资产文件夹中的文件名列表。

where pics[i] is a list of filenames which sits in my Asset folder.

在code对我的作品,但我仍然不时接收到错误的时间,从用户(我看到它在开发者控制台错误):

The code works for me, but i still receive from time to time Errors from users (i see it on Developer Console errors):

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)

有什么我可以做的改进呢?或者这是Android的世界,我们永远不能下发一个完美的应用?

Is there anything i can do to improve it? or this is Android's world, we can never deliever a perfect application?

推荐答案

当然,你可以改善的东西:不要过多的图片加载到在同一时间内存(或减少其大小)。有些设备没有太多的可用内存。您的设备可能有较高的堆限制比一些对你的客户电话。因此它崩溃了他们,当你的作品(见这个影片一些堆限制[在4:44])。

Of course you can improve something: Don't load too much pictures into the memory at the same time (or reduce their size). Some devices don't have much memory available. Your device may have a higher heap limit than some of your customers phones. Therefore it crashes for them when it works for you (see this video for some heap limits [at 4:44]).

您可以通过 <$获得可用堆大小C $ C> ActivityManager.getMemoryClass()

You may get the available heap size via ActivityManager.getMemoryClass().

要改善这一点,测试,图片你现在需要(其中显示),而且它不是。只加载了必需的和回收的位图就不需要了。

To improve this, test which pictures you need right now (which are displayed) and are which not. Load only the required ones and recycle the bitmaps you don't need anymore.

也可以尝试使用 BitmapFactory.de codeResource() BitmapFactory.Options.inSampleSize 。这使您可以加载在一个较低分辨率图像的情况下直接在全尺寸加载它们,然后调整他们为你在这里做。

Also try using the BitmapFactory.decodeResource() and BitmapFactory.Options.inSampleSize. This allows you to load images at a lower resolution directly without loading them at the full size and then resizing them as you did here.

这篇关于位图大小超过Android上的VM预算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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