【问题标题】:getting java.lang.OutOfMemoryError with small amount of images, is it better to use pngs or bitmaps?使用少量图像获取 java.lang.OutOfMemoryError,使用 png 还是位图更好?
【发布时间】:2012-05-10 19:11:48
【问题描述】:

我有一些图片正在加载到我的程序中,这些图片似乎会导致 OutOfMemoryError。我更喜欢使用 .png 文件,因为它们有一个清晰的 alpha 层,但根据this SO link,最好对图像使用 BitmapFactory 命令以避免 OoME。是否可以对 .png 文件使用 BitmapFactory 命令?是坚持使用 .png 文件还是尝试将它们全部转换为位图或其他图像格式更好?只有大约 39.3 万张图片。

错误代码:

05-01 03:07:36.197: E/AndroidRuntime(561): Caused by: java.lang.OutOfMemoryError
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.Bitmap.nativeCreate(Native Method)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
05-01 03:07:36.197: E/AndroidRuntime(561):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

编辑:我的图像加载了一种自定义图像视图

 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="bottom"
     android:layout_weight="0.55"
     android:id="@+id/dicemenu">

     <com.surreall.yacht.SquareButton        
        android:layout_height="fill_parent"
        android:layout_width="0dip"          
        android:layout_weight="1"
    >
        <ImageView
        android:id="@+id/die1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="wrap_content"
        android:layout_width="0dp"          
        android:layout_weight="1" 
        android:layout_margin="9dp" 
        />

      </com.surreall.yacht.SquareButton>

自定义图像视图看起来像这样(说来话长,是我从found here 问题中得到的解决方案:

package com.surreall.yacht;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class SquareButton extends LinearLayout {

    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // This is used to make square buttons.
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

【问题讨论】:

标签: android bitmap out-of-memory


【解决方案1】:

当你不再需要你的图片时,尝试做Bitmap.recycle()

【讨论】:

  • 一旦我的带有图像的 xml 布局被加载,它就会得到错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 2016-05-11
  • 2015-12-13
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
相关资源
最近更新 更多