1

I am try to converting image to bitmap using following bit of code:

//Here file is the image file
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());

Now the problem is some times i got the OutOfMemoryError. I don't know when the error exactly happened.

My simple solution add try catch add and avoid crash. I know it is not the right way.

How to handle OutOfMemoryError here?

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • ran out of memory. Scale down the image – Raghunandan Aug 22 '14 at 08:27
  • You can try to play around with the BitmapFactory.Options. But it is possible that the file is too big to load in memory as a Bitmap. – Joris Aug 22 '14 at 08:27
  • you can take a look at [Out of memory error in android bitmap](http://stackoverflow.com/questions/24203985/out-of-memory-error-in-android-bitmap/24204533#24204533) – Kaushik Aug 22 '14 at 08:54
  • @Raghunandan How to do scaling? – Amsheer Aug 22 '14 at 09:27
  • @Joris yes i am handling big files so how to avoid this – Amsheer Aug 22 '14 at 09:28
  • search google stack overflow – Raghunandan Aug 22 '14 at 09:31
  • @Amsheer did you read the documentation for BitmapFactory.Options? As is also statedx in the answer there is a field inSampleSize with the description "If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory." – Joris Aug 22 '14 at 09:32
  • I mean i am also use decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight){} method is it what you mean? In some other place – Amsheer Aug 22 '14 at 09:34

1 Answers1

0

Add this options in decode in order to optimize memory:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPurgeable = true;
bmOptions.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), bmOptions);
Héctor
  • 24,444
  • 35
  • 132
  • 243