简体   繁体   中英

Image Bitmap gets rotated when loaded from camera/gallery [Android 9]

I am loading the image from gallery or camera so that I can edit it. I have used EXIFInterface to manage the rotation of bitmap. But in Samsung s8[Android 9], it provides 90 degree rotation for the image but Image is originally not rotated. And depending on this rotation I rotate it 90 degree which I don't want.

I have tried using ContentResolver to get rotation through cursor but it also has same problem as EXIFInterface. Following are the both methods I tried to fix the problem:

private static int getExifOrientation(String image_absolute_path) throws IOException {
    ExifInterface ei = new ExifInterface(image_absolute_path);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
                return RotationOptions.ROTATE_90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return RotationOptions.ROTATE_180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return RotationOptions.ROTATE_270;
            default:
                return RotationOptions.NO_ROTATION;
    }
}

 private static int getOrientation(Context context, Uri photoUri) {
        try {
            Uri imageContentUri = getImageContentUri(context, photoUri.getPath());
            if (imageContentUri == null) {
                return -1;
            }
            Cursor cursor = context.getContentResolver().query(imageContentUri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
            if (cursor == null) {
                return -1;
            }
            if (cursor.getCount() != 1) {
                cursor.close();
                return -1;
            }
            cursor.moveToFirst();
            int orientation = cursor.getInt(0);
            cursor.close();
            cursor = null;
            return orientation;
        } catch (Exception e) {
            return -1;
        }
    }

I was also facing same problem. I managed without EXIFInterface,

By rotating image with this condition -

if (bm.getWidth() > bm.getHeight()) {
            bm = rotateImage(bm, 270);
        }
        bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM