简体   繁体   English

Android:加载图片时出错:OutOfMemoryError:位图大小超过VM预算

[英]Android: Error loading images: OutOfMemoryError: bitmap size exceeds VM budget

I'm developing an Android Application for a tablet (1024x768). 我正在为平板电脑开发Android应用程序(1024x768)。 I searched and find some solutions here, but anyone solved me the problem. 我在这里搜索并找到了一些解决方案,但任何人都解决了我的问题。 The application has 5 screens and each screen has an ImageView that I load: 该应用程序有5个屏幕,每个屏幕都有一个我加载的ImageView:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="1024dp"
    android:layout_height="768dp"
    android:layout_centerHorizontal="TRUE"
    android:src="@drawable/scene1"
    />

The first and the second screen loads good, but when I try to load the third one, I get the following errors (Every screen I load by startActivity which its layout. It's not a layout error, because if I load them with a different order, I can load two and the third one crases too): 第一个和第二个屏幕加载良好,但是当我尝试加载第三个屏幕时,我得到以下错误(每个屏幕我由startActivity加载它的布局。这不是布局错误,因为如果我用不同的顺序加载它们,我可以加载两个,第三个也是旧的):

05-28 17:03:51.774: E/AndroidRuntime(401): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>

05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>


05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: java.lang.reflect.InvocationTargetException


05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I tried several things: 我尝试了几件事:

 @Override
  public void onPause()
  {
    super.onPause();
    Log.i("URI", "Syste.gc");
    // yada yada yada...

    System.gc();
  }

and

@Override
protected void onDestroy()
{
    super.onDestroy();

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
}

private void unbindDrawables(View view)
{
    if (view.getBackground() != null)
    {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup)
    {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
        {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

but I always get the error. 但我总是得到错误。

The images are PNG and its weight is lower than 50k. 图像为PNG,重量低于50k。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

1024x768 is the dimensions in DP, the number of pixels could be up to twice that on a high resolution device. 1024x768是DP中的尺寸,像素数可能是高分辨率设备的两倍。

If you decode a 1024x768 image with a bitmapfactory it will scale the bitmap relative to density, so you could end up with a 2048x1536 image, which would take up something like 12MB. 如果您使用bitmapfactory解码1024x768图像,它将相对于密度缩放位图,因此您最终可能会得到2048x1536图像,这将占用12MB。

If you always want the image to be the same resolution regardless of device resolution, than put it in the Drawable-nodpi folder, that will cause it not to be resized on decode. 如果您始终希望图像的分辨率与设备分辨率无关,而不是将其放在Drawable-nodpi文件夹中,这将导致它在解码时不会调整大小。

Try to load the image in the onCreate() method instead of loading it from the XML. 尝试在onCreate()方法中加载图像,而不是从XML加载它。

Have you tried to first read the dimensions and type of the image, and then load a scaled down version of that image? 您是否尝试首先阅读图像的尺寸和类型,然后加载该图像的缩小版本?

As the Android developer resources suggest. 正如Android开发人员资源所示。

first of all, calling System.gc() won't really make the Garbage Collector pass, and it can really hinder your performance so I'd restrain myself from using it so widely. 首先,调用System.gc()不会真正使垃圾收集器通过,它可以真正阻碍你的性能,所以我会限制自己如此广泛地使用它。 If you want the garbage collector to clean the images, use WeakReferences instead, and store the images on a HashMap. 如果您希望垃圾收集器清理图像,请改用WeakReferences,并将图像存储在HashMap中。

Second of all, try to use smaller images, as the VM will quickly collapse instead. 其次,尝试使用较小的图像,因为VM会迅速崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM