Search code examples
androidout-of-memoryandroid-imageimage-gallery

how device gallery show large images?


Whenever I try to show large size(1 MB) bitmap in imageview, system throw OutOfMemory exception.

If I place 7-8 MB image, gallery can easily show that image.

Just want to know How device default gallery is able to show large image in easy way?

  1. Which mechanism is used by device gallery?

Solution

  • Generally speaking, they subsample the image. A 1MB PNG or JPEG file will be much higher resolution than the screen, and so it is wasteful to load the whole image in. Subsampling allows you to load the image in much closer to the actual size of the ImageView you will use, allowing it to fit in memory better. A simple approach to subsampling involves using inSampleSize on BitmapFactory.Options; Dave Morrissey's SubsamplingScaleImageView offers pinch-to-zoom and such while peforming subsampling (note: I have not tried this component, as I just ran across it a minute ago).