从相机/图库加载图像位图后会旋转[Android 9]

时间:2019-06-24 11:57:07

标签: java android android-camera android-bitmap

我正在从图库或照相机中加载图像,以便可以对其进行编辑。我已经使用EXIFInterface来管理位图的旋转。但是在Samsung s8 [Android 9]中,它为图像提供了90度旋转,但是图像最初并未旋转。并根据此旋转将其旋转90度,这是我不希望的。

我尝试使用ContentResolver通过光标进行旋转,但是它也存在与EXIFInterface相同的问题。以下是我尝试解决问题的两种方法:

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;
        }
    }

1 个答案:

答案 0 :(得分:0)

我也面临着同样的问题。我在没有EXIFInterface的情况下进行管理,

通过在这种情况下旋转图像-

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