BitmapFactory java.lang.OutOfMemoryError (BFOME)

Hi guys, back again with me. Okay, this time I will share another error case, when I was making an image upload feature, actually it had not reached the upload stage, it was still at the image pick stage from the gallery, now this process will involve something called BitmapFactory.

Okay, in principle what I did at that time was correct, and it worked smoothly in other Android projects. However, over time the libraries and Android studio continued to update and I allowed it because the notification was too distracting to my view lol.  I mean, that might be what caused the error tolerance to be tightened .

Long story short, the error line number points to my following code.

//KODE KASUS
Bitmap bm = BitmapFactory.decodeFile(strPath);
imageView.setImageBitmap(bm);

Well, there are 2 methods to fix it, first using recycle(), so our bitmap container object is recycled. The way to do this is to create a condition when the bitmap is not null, then execute the recycler, this will free your memory in each iteration. Put this code under CASE CODE.

if(bm!=null){
   bm.recycle();
   bm=null;
}

Another way is to apply options.inSampleSize. Do this before the CASE CODE.

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;

Bitmap bm = BitmapFactory.decodeFile(strPath,options);
imageView.setImageBitmap(bm);

Use this method when you want to save memory and lighten the load on the decoder, besides it can load bitmaps faster and more efficiently.


Post a Comment

Previous Next

نموذج الاتصال