java.lang.OutOfMemoryError - BitmapFactory.decode(strPath)
Solution 1:
You need to recycle Bitmap object .
Bitmap bm = BitmapFactory.decodeFile(strPath);
imageView.setImageBitmap(bm);
After above lines of code in your get view just add the code written below ///now recycle your bitmap this will free up your memory on every iteration
if(bm!=null)
{
bm.recycle();
bm=null;
}
After this also if you are getting same error the
Replace below code
Bitmap bm = BitmapFactory.decodeFile(strPath);
imageView.setImageBitmap(bm);
with
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bm = BitmapFactory.decodeFile(strPath,options);
imageView.setImageBitmap(bm);
Use inSampleSize to load scales bitmaps to memory. Using powers of 2 for inSampleSize values is faster and more efficient for the decoder. However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.
For more see Loading Large Bitmaps Efficiently
Solution 2:
Use this in manifest file in Application tag
android:largeHeap="true"
Solution 3:
before calling Bitmap bm=BitmapFactory.decodeFile(strPath);
call this.. Bitmap bm =decodeSampledBitmapFromResource(strPath,reqWidth,reqHeight);
if you get again java.lang.OutOfMemoryError then let me know
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String strPath,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(strPath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options,reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(strPath, options);;
}
Solution 4:
It's an optimisation problem for your application. You are getting OutOfMemoryError because when you are doing BitmapFactory.decodeFile(strPath) android is trying to allocate memory for that bitmap. In your case system can't find enough free space to allocate and that's why you are getting this error.
Now as i can see from your code you are trying to show list of images using ImageAdapter. In that case your imageview must have smaller width and height than actual image.
To give you a more generalised idea here's what is happening:
- ImageView width * height = 100dp * 100dp
- Image width * height = 800px * 800px.
In this scenario though our imageview width-height is 100 * 100 but we are trying to set 800*800 image as it's background. And this definitely is not an efficient way because the system will allocate memory for 800*800px image whereas 100*100 would do.
That's why before you do any decoding of bitmap you should sample the bitmap so that only 100*100 worth of memory is allocated.
You will find a more detail version of this explanation here
Solution 5:
I had the same problem. I incorporated all the above suggestions, but I still had the problem of java.lang.outOfMemoryError. After a lot of tweaking around, what finally worked was scaling down image dimensions (and size). Originally, I had images of dimensions 800 x 1000 sorts. I rescaled them to about 60 x 80 (because that's what I needed), and it worked!
So besides following all advice you get on stackoverflow and other sites about this issue, also do a grassroots check of your image sizes and dimensions. Will save you a lot of headache.