我有一个具有图像上传功能的应用程序,在上传图像后,onActivityResult()
我收到图像的 Uri 并将其转换为具有以下功能的位图:
private fun uriToBitmap(selectedFileUri: Uri): Bitmap? {
return try {
val parcelFileDescriptor: ParcelFileDescriptor =
requireContext().contentResolver.openFileDescriptor(selectedFileUri, "r")!!
val fileDescriptor: FileDescriptor = parcelFileDescriptor.fileDescriptor
val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
parcelFileDescriptor.close()
image
} catch (e: IOException) {
e.printStackTrace()
null
}
}
但由于某种原因,如果它是肖像图像,我的图像会旋转 90 度
我已经尝试使用这个ExifInterface
东西来修复它并使用这个功能将它旋转回来:
fun determineImageRotation(imageFile: File, bitmap: Bitmap): Bitmap {
val exif = ExifInterface(imageFile.absolutePath)
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)
val matrix = Matrix()
when (orientation) {
6 -> matrix.postRotate(90f)
3 -> matrix.postRotate(180f)
8 -> matrix.postRotate(270f)
}
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
但我收到此错误:
ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface.
java.io.EOFException
这就是我为图像创建文件路径的方式:
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String =
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
val storageDir = requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"PNG_${timeStamp}_", /* prefix */
".png", /* suffix */
storageDir /* directory */
)
}
这是我为图像提供的文件路径的示例:
/storage/emulated/0/Android/data/avedot.app/files/Pictures/PNG_20210622_094219_232594250744276112.png
我假设路径中有一些与点相关的东西avedot.app
破坏了 ExifInterface 函数,但我该如何解决这个问题?
提前致谢 !