0

My application gets crashed on opening a .png file which is having size of 2.5MB. This file is of high resolution.Im getting OOM error.

The piece of code which causes the error is:

long sizeInMB = imageFile.length()/(1024*1024);
if(sizeInMB > 2){
    int inSampleSize = (int) (sizeInMB/5);
    if(sizeInMB%5 > 0){
        inSampleSize++;
    }
    Options opts = new Options();
    opts.inSampleSize=inSampleSize;
    bitmap = BitmapFactory.decodeFile(filePath, opts);    
}else{
    bitmap = BitmapFactory.decodeFile(filePath);
} 

The line bitmap = BitmapFactory.decodeFile(filePath) causes the error.

Any help will be appreciated,

Thanks in advance

Nate
  • 31,017
  • 13
  • 83
  • 207
Rino
  • 1,215
  • 1
  • 16
  • 28
  • If `bitmap = BitmapFactory.decodeFile(filePath)` causes the error something seems to go wrong because the other if-branch (`bitmap = BitmapFactory.decodeFile(filePath, opts);`) should be executed, shouldn't it? – Taig Feb 08 '13 at 05:27

2 Answers2

2

Because in your case 2MB bitmap might take around 10MB of memory

refer this on how to load large bitmaps

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
1

Try using this may be it will solve your problem.

Options opts = new Options(); 
  opts.inJustDecodeBounds = true; 
  BitmapFactory.decodeFile(path, opts); 
  Log.e("optwidth",opts.outWidth+""); 
  opts.inJustDecodeBounds = false; 
  if(opts.outWidth>500){ 
          opts.inSampleSize = 4; 
          mBitmap = BitmapFactory.decodeFile(path, opts); 
  } 
  else mBitmap = BitmapFactory.decodeFile(path, opts);

I have added code for the width size, you can also add as per your requirement for height.

if you want more information then check this link it will help you: Handling large Bitmaps

Other suggested link is here: Out of memory cache error when accessing inside the app

Community
  • 1
  • 1
Maulik
  • 3,316
  • 20
  • 31