Android: “java-lang-outofmemoryerror-bitmap-size-e

2019-02-21 04:17发布

问题:

In my application I have to display an image which is about 10000 x 5000 , using small images of 500x500 px. I.e;thru tiling images. In order to tile all images to one initially am collecting all bitmaps from sdcard in order to draw on canvas.

But while am loading images from extenal storage using BitmapFactory.decodeFile(path) i encounterd with error java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android

can any one how to avoid this error and gimme an idea how to tile lot of images in to a single image .

Thanking you, Srinivas

回答1:

  1. Never load any tiles that you don't need. "am collecting all bitmaps from sdcard" suggests that you are decompressing every image you might ever need, which would be the wrong approach.

  2. Use smaller tiles. 256x256 is a good size.

  3. Load them as Config.RGB565 (i.e. 16bpp).

  4. Regularly recycle() Bitmaps you don't need.

Basically you need to implement some sort of most-recently-used (MRU) Bitmap cache.