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
 
     
    