1

i have some pictures that i am loading into my program that seem to be causing a OutOfMemoryError. i would prefer using .png files since they have a clear alpha layer, but according to this SO link it might be better to use a BitmapFactory command for images to avoid the OoME. is it possible to use the BitmapFactory command on .png files? is it better to stick with .png files or try to convert them all to bitmaps or some other format for images? there is only about 393k worth of images.

error code:

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)

edit: my images are loaded with a sort of custom imageview

 <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>

and the custom imageview looks like this (was sort of a long story, was a solution i got from a problem 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);
    }
}
Community
  • 1
  • 1
clayton33
  • 4,176
  • 10
  • 45
  • 64

1 Answers1

1

Try to do Bitmap.recycle() when you no longer need your image

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158