java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I suppose may be this post will help you
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
Related videos on Youtube
mlevit
Updated on May 06, 2022Comments
-
mlevit about 2 years
So I've got a lazy image loader for my
ListView
. I also use this tutorial for better memory management and haveSoftReference
Bitmap images stored in myArrayList
.My
ListView
works loads 8 images from a DB then once the user scrolls all the way to the bottom it loads another 8 etc etc. There was no issue when there were around 35 images or less, but any more and my app Force Closes withOutOfMemoryError
.The thing that I can't understand is I have my code inside a try catch:
try { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(image, 0, image.length, o); //Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while(true) { if(width_tmp/2 < imageWidth || height_tmp/2 < imageHeight) { break; } width_tmp/=2; height_tmp/=2; scale++; } //Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length, o2); } catch (Exception e) { e.printStackTrace(); }
But the try catch block isn't catching the
OutOfMemory
exception and from what I understand theSoftReference
Bitmap images should be cleared when the application is running out of memory stopping theOutOfMemory
exception being thrown.What am I doing wrong here?
-
Pascal over 11 yearsShould be the select solution!