0

I want to show my images (stored in drawable folder) in gridview. There are 12 images. For that i made a ImageAdapter class. When i open my GridViewActivity my app crashes by saying "Out of Memory: Bitmap size exceeds VM Budget". Here is the code.

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

Context context;

Integer[] imageIDs = { R.drawable.lw01_04_001, R.drawable.lw01_04_002,
        R.drawable.lw01_04_003, R.drawable.lw01_04_004,
        R.drawable.lw01_04_005, R.drawable.lw01_04_006,
        R.drawable.lw01_04_007, R.drawable.lw01_04_008,
        R.drawable.lw01_04_009, R.drawable.lw01_04_010,
        R.drawable.lw01_04_011, R.drawable.lw01_04_012, };

public ImageAdapter(Context ctx) {
    context = ctx;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imageIDs.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView;

    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 5, 5, 5);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(imageIDs[position]);
    return imageView;
  }
}

GridViewActivity.java

public class GridViewActivity extends ActionBarActivity {

private Utils utils;
private ImageAdapter imageAdapter;
private GridView gridView;
private int columnWidth;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid_view);

    context = this;
    gridView = (GridView) findViewById(R.id.grid_view);

    utils = new Utils(this);

    // Initilizing Grid View
    InitilizeGridLayout();

    imageAdapter = new ImageAdapter(context);

    // setting grid view adapter
    gridView.setAdapter(imageAdapter);
}

2 Answers2

0

Your images are probably too large. When you give an ImageView an image resource ID, the ImageView generates a bitmap of that resource, then holds it in memory. While most modern devices are built with enough memory to easily handle multiple large images, Android allocates a very small amount of that memory to each app. This means that you need to be clever when you work with images, especially when you need to display many of them.

Here are some options:

  1. Make sure the images in your drawable folder aren't bigger than they need to be (i.e., they should be no larger than the dimensions of the view that's displaying them). This is probably the best option, since in addition to decreasing your memory requirements, you'll also be reducing the size of your resulting APK file.

  2. If you don't want to scale down your images in your drawable folder (for example, you might need to show them in full size if the user clicks them), scale down the images during runtime. This option will require a bit more code, but gives you the flexibility of displaying images exactly how you need to, without running into out of memory issues.

Here's a code example for option #2:

 Options opts = new Options();
    opts.outWidth = 85; //Based on your size requirement.
    opts.outHeight = 85;
    Bitmap b = BitmapFactory.decodeResource(getResources(), imageIDs[position], opts);
    imageView.setImageBitmap(b);

This should get you started.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
-1

Your Bitmaps are probably too big.

I suggest you check this question: Out of Memory Error ImageView issue

radzio
  • 2,862
  • 4
  • 26
  • 35