0

I have a Image view in my application with save button. When we click on the save button the app will show some sizes when the user select any size the image wan to save to device external device. Working fine in some(low) sizes but for large sizes it is force closing. I am facing the memory out of exception. I dont want to lose the quality of the image.

Please any one help in this

Krishna
  • 4,892
  • 18
  • 63
  • 98

1 Answers1

0

Use BitmapFactory.Options to save image without oom error like below.

final int DESIRED_WIDTH = 640;

// Set inJustDecodeBounds to get the current size of the image; does not
// return a Bitmap
final BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
Log.d(TAG, "Bitmap is " + sizeOptions.outWidth + "x"
            + sizeOptions.outHeight);

// Now use the size to determine the ratio you want to shrink it
final float widthSampling = sizeOptions.outWidth / DESIRED_WIDTH;
sizeOptions.inJustDecodeBounds = false;
// Note this drops the fractional portion, making it smaller
sizeOptions.inSampleSize = (int) widthSampling;
Log.d(TAG, "Sample size = " + sizeOptions.inSampleSize);

// Scale by the smallest amount so that image is at least the desired
// size in each direction
final Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length,
        sizeOptions);
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36