0

When scrolling back and forth a bunch of times on the image gallery my app crashes with:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I need the gallery to show two images vertically:

  • The top one will be a selection from a pre-defined group of images in the res/drawable folder.
  • The bottom image will be a green check mark if they answered this particular image correct (this is the submit your score page of the game) or a red circle with a line through it if they get it wrong.

Here is my code that extends the BaseAdapter:

    public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.SubmitScoreGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.SubmitScoreGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }
    @Override
    public int getCount() {

        return numQuestions;
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        //Setup a LinearLayout to display the second image
        LinearLayout lLayout = new LinearLayout(this.mContext);
        lLayout.setOrientation(LinearLayout.VERTICAL);
        //Create the ImageView
        ImageView imageView = new ImageView(this.mContext);


        imageView.setImageResource(imageList.get(randOrder.get(position)));
        imageView.setLayoutParams(new Gallery.LayoutParams(gDispW, gDispH));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);
        lLayout.addView(imageView);
        //Create the right/wrong image
        ImageView imageViewBottom = new ImageView(lLayout.getContext());
        if (score.getScoreAtIndex(position)== 1){
            imageViewBottom.setImageResource(R.drawable.green_checkmark);   
        }
        else{
            imageViewBottom.setImageResource(R.drawable.incorrect_circle);
        }
        imageViewBottom.setLayoutParams(new Gallery.LayoutParams(gDispW, gDispH));
        imageViewBottom.setPadding(gDispW/3, 0, gDispW/3, gDispH/2);
        imageViewBottom.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        //imageViewBottom.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        lLayout.addView(imageViewBottom);

       return lLayout;

    }
}

randOrder is an array that holds the order of the images

The gallery holds 5, 10 or 15 images depending on how many questions the user chooses.

I can get it to crash consistently with the 15 images.

Is there a better way to do this?

What am I doing wrong?

Thank you,

Neil

Neil Hoff
  • 2,025
  • 4
  • 29
  • 53

2 Answers2

2

On getView method, you create an ImageView every time. You can use convertView like this:

        ImageView imageView;
        // if it's not recycled, initialize some attributes
        if (convertView == null) {
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }

Moreover, you can use resampling on your bitmaps.

Timuçin
  • 4,653
  • 3
  • 25
  • 34
  • I tried doing this for the imageView, lLayout and imageViewBottom but it still crashes. – Neil Hoff Oct 01 '11 at 22:37
  • have you tried resampling bitmaps? check this question: http://stackoverflow.com/questions/3331527/android-resize-a-large-bitmap-file-to-scaled-output-file – Timuçin Oct 02 '11 at 09:00
0

Comment 19 of this post: code.google.com Issue 8488 fixed the issue.

All I had to do was create the folder res/drawable-nodpi and put the images that I am calling in there.

For some reason if you store your images in res/drawable the attr.recycle() doesn't work right. Anyone know why?

I also implemented Tim's suggestion and it seems to have sped up the scrolling back and forth in the gallery. Thank you!

EDIT:

The above answer worked for a myTouch 4g but the program still crashed with a G2. So I took Tim's advice and started to look at re-sampling images. I ended up using Fedor's answer from this post: Strange out of memory issue while loading an image to a Bitmap object and I haven't seen the out of memory issues on the G2 since.

Community
  • 1
  • 1
Neil Hoff
  • 2,025
  • 4
  • 29
  • 53