我有一个理解问题。我拍了一张照片,然后想以正确的方向显示它。如果我在模拟器上运行整个过程,一切正常。只有在正确的设备上,纵向模式下的图片始终显示为水平旋转并保存在图库中。
val rotatedBitmap = getPictureBitmap(path,capturedImg!!)
private fun getPictureBitmap(yourPathToPicture: String?, bitmap:Bitmap): Bitmap? {
val ei: ExifInterface = ExifInterface(yourPathToPicture!!)
val orientation = ei.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED
)
var rotatedBitmap: Bitmap? = null
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotatedBitmap = rotateImage(bitmap, 90)
ExifInterface.ORIENTATION_ROTATE_180 -> rotatedBitmap = rotateImage(bitmap, 180)
ExifInterface.ORIENTATION_ROTATE_270 -> rotatedBitmap = rotateImage(bitmap, 270)
ExifInterface.ORIENTATION_NORMAL -> rotatedBitmap = bitmap
else -> rotatedBitmap = bitmap
}
return rotatedBitmap
}
...
val rotatedBitmap = getPictureBitmap(path,capturedImg!!)
//Correct format in the emulator. It will also be saved to the gallery in the correct format. But not on the real device
MediaStore.Images.Media.insertImage(contentResolver, rotatedBitmap, null , null);