I hope my solution would still help,
byte[] data = null;
Bitmap bitmap = BitmapFactory.decodeByteArray(fileBytes, 0, fileBytes.length);
ByteArrayOutputStream outputStream = null;
try {
bitmap = ImageResizer.rotateImageIfRequired(bitmap, context, uri);
outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
data = outputStream.toByteArray();
} catch (IOException e) {
Timber.e(e.getMessage());
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
// Intentionally blank
}
}
return data;
}
public static Bitmap rotateImageIfRequired(Bitmap img, Context context, Uri selectedImage) throws IOException {
if (selectedImage.getScheme().equals("content")) {
String[] projection = { MediaStore.Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (c.moveToFirst()) {
final int rotation = c.getInt(0);
c.close();
return rotateImage(img, rotation);
}
return img;
} else {
ExifInterface ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Timber.d("orientation: %s", orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
return rotatedImg;
}