为什么我在Android中获得OutOfMemory Exception?

时间:2015-06-02 18:02:06

标签: android gridview imageview

我正在使用Android GridView。我有大约60个.png图像,每个图像大小> 100KB。我制作了一个适配器并将该适配器设置为GridView。我按照Google android documentation的教程进行了操作。当我在模拟器中运行应用程序时,应用程序抛出了如下错误:

 java.lang.OutOfMemoryError: Failed to allocate a 4976652 byte allocation with 982448 free bytes and 959KB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
        at android.content.res.Resources.loadDrawable(Resources.java:2330)
        at android.content.res.Resources.getDrawable(Resources.java:758)
        at android.content.Context.getDrawable(Context.java:402)
        at android.widget.ImageView.resolveUri(ImageView.java:741)
        at android.widget.ImageView.setImageResource(ImageView.java:397)

我将ImageView与图片设置为:

imageView.setResources(R.drawable.imagename);

我的模拟器具有> 700MB的可用RAM大小。我不明白为什么我会超越异常。

此外,在Android中设置图像的标准方法是什么?

注意:当我将图像数量减少到4-5左右时,该应用程序运行正常。

0 个答案:

没有答案