从ExifInterface获取旋转始终返回0

ono*_*ono 5 camera android exif bitmap

我正在通过onActivityResult相机传递一个位图.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "picture");
mCapturedImageURI =     getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

startActivityForResult(intent, REQUEST_TAKE_PHOTO); 
Run Code Online (Sandbox Code Playgroud)

我可以得到位图:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mCapturedImageURI);
Run Code Online (Sandbox Code Playgroud)

但是,我注意到图像在某些设备上旋转.在这里搜索帖子后,典型的解决方案似乎通过以下方式获得轮换:

String path = mCapturedImageURI.getPath();
ExifInterface exif = new ExifInterface(path);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Run Code Online (Sandbox Code Playgroud)

不幸的是,int rotation即使位图旋转,我总是0.

我也试过这个,当我在设备上已经上传了一张图片但方向仍为0时有效:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(mCapturedImageURI, orientationColumn, null, null, null);
if (cur != null && cur.moveToFirst()) {
      orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Run Code Online (Sandbox Code Playgroud)

有人看到我在这里做错了吗?还是另一种解决方法?

通常,位图与后置摄像头逆时针旋转90度,前置摄像头顺时针旋转90度.在Moto G上运行正常.在Galaxy S3和LG G2上旋转.

Asw*_*ran 6

尝试使用内容光标中的信息.

float photoRotation = 0;
boolean hasRotation = false;
String[] projection = { Images.ImageColumns.ORIENTATION };
try {
    Cursor cursor = getActivity().getContentResolver().query(photoUri, projection, null, null, null);
    if (cursor.moveToFirst()) {
        photoRotation = cursor.getInt(0);
        hasRotation = true;
    }
    cursor.close();
} catch (Exception e) {}

if (!hasRotation) {
    ExifInterface exif = new ExifInterface(photoUri.getPath());
    int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_UNDEFINED);

    switch (exifRotation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            photoRotation = 90.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            photoRotation = 180.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            photoRotation = 270.0f;
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)