java.lang.OutofMemoryError: bitmap size exceeds VM budget. This is most common error for us when we are decoding Bitmap more than 4 MB. In generally before decoding bitmap we do not know how much bigger the size of a bitmap will be. So its very difficult problem for us to handle this error in proactive approach But good thing is that android provide a way to handle this problem.Before decoding bitmap we just decode it with options.inJustDecodeBounds = true. options is the instance of BitmapFactory.
It does not load bitmap into memory but it help us to find the width and height of a bitmap so that we can reduce the height and width according to our device
As Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.
So we will find actual height and width of a bitmap as follows...
Here inSampleSize will reduce the size and memory size of an Bitmap.To use this method, first decode withinJustDecodeBounds set to true, pass the options through and then decode again using the newinSampleSize value and inJustDecodeBounds set to false.
Memory over flow error is gone…….