In Android, how do you display an image (of any size) from the SD card, without getting an out of memory error?
Is it necessary to put the image in the Media Store first?
A pseudo-code example would be greatly appreciated. Extra points if the displayed image is as big as the memory level of the device allows.
Edit: this question has actually been already answered at Strange out of memory issue while loading an image to a Bitmap object (the two highest voted answers). It does also use the
inSampleSizeoption, but with a small method to automatically get the appropriate value.My original answer:
The
inSampleSizeof theBitmapFactory.Optionsclass can solve your issue (http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize). It works by making a bitmap with the resulting width and height 1/inSampleSizethan the original, thus reducing memory consumption (byinSampleSize^2?). You should read the doc before using it.Example:
However here it will only work for images up to, I guess,
inSampleSize^2 times the size of the allowed memory and will reduce the quality of small images.The trick would be to find the appropriate inSampleSize.