In below code, I am getting exception "Out of memory on a byte allocation" for a large size images in function "getScaledBitmap" when processing decodeFile second time in code. This all below function is being called 4 times, as I am processing 4 different images on a screen.
Please guide on this.
private Bitmap processimage(String picturePath){
Bitmap thumbnail =null;
thumbnail=getScaledBitmap(picturePath,500,500);
Matrix matrix = null;
try {
ExifInterface exif = new ExifInterface(picturePath);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = bal.exifToDegrees(rotation);
matrix = new Matrix();
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
thumbnail=Bitmap.createBitmap(thumbnail, 0,0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
}
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return thumbnail;
}
protected Bitmap getScaledBitmap(String picturePath, int width, int height) {
Bitmap result=null;
try{
BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, sizeOptions);
int inSampleSize = calculateInSampleSize(sizeOptions, width, height);
sizeOptions.inJustDecodeBounds = false;
sizeOptions.inSampleSize = inSampleSize;
result=BitmapFactory.decodeFile(picturePath, sizeOptions);
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
protected int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
|| (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}