0

In my application, i have 3 screens containing multiple high resolution images. The number of images used in a screen is around 70-75. I have written the code to add images in a grid layout using an adapter class extending BaseAdapter, in the getView() method i wrote the code,

adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);

int x = (int)(width/5.1f);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(x,x));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 20, 4, 20);

but while loading this screen, it show lots of memory issues, and in logcat i am getting the error,

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Please share how could i write the code to handle memory issues with multiple high resolution images. Thanks.

Rajesh
  • 45
  • 6
  • 1
    70-75... what are surprise, huh? ) Place here code of your ImageAdapter – Dmitry Zaytsev Mar 28 '12 at 13:08
  • 1
    You should use thumbnails, only solutions... – Ferdau Mar 28 '12 at 13:18
  • check this link also http://stackoverflow.com/questions/4611822/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget – user1298288 Mar 28 '12 at 13:39
  • 1
    As Ferdau said, you should generate smaller sized images (thumbnails) to display as grid, and only display full res image when users require it (by clicking on an image for example). This will solve your memory issue and run much faster. – Kai Mar 28 '12 at 13:44
  • ya thats right. thanks Ferdau and kai. – Rajesh Mar 28 '12 at 13:58

1 Answers1

0

If there is no way to adjust the images resolutions you should open them as BitmapFactory.Options().inJustDecodeBounds = true, pass your options to the images (desired adjusted size) and then decode them again using BitmapFactory.Options().inJustDecodeBounds = false.

The actual byte size of a bitmap image is calculated by multiplying the number of pixels by then number of bytes allocated for the pixel. ARGB_8888 (which is recommended) allocates 4 bytes per pixel, therefore, the size will be width * height * 4 Bytes.

For more details read the Loading Large Bitmaps Efficiently lesson from Android. Also this post should help.

Community
  • 1
  • 1
slybloty
  • 6,346
  • 6
  • 49
  • 70