问题描述
嗨,我有一个带有compileSdkVersion 30和targetSdkVersion 30的应用程序。 由于我需要了解图像的方向,因此我写了这些:
val exif = ExifInterface(imageFile.absolutePath)
val orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_norMAL
)
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> rotate = 270
ExifInterface.ORIENTATION_ROTATE_180 -> rotate = 180
ExifInterface.ORIENTATION_ROTATE_90 -> rotate = 90
}
java.io,FileNotFoundException:/storage/emulated/0/DCIM/Camera/xxx.jpg: open Failed EACCESS(Permission denied)
...
at android.media.ExifInterface.<init>(ExifInterface.java.1389)
我想做的就是获取图像并知道其方向,但是我在Internet上找不到任何示例。有人可以给我提示吗?谢谢!
解决方法
在Android 11上,您可以访问Camera目录,但大多数情况下不能访问该目录中属于其他应用的文件。
如果使用经典文件系统路径,则不会。
那么哪个应用将这些文件放在那里?
如果不是,那么您可以使用ACTION_OPEN_DOCUMENT之类的操作让用户选择一个文件,为您提供一个不错的uri。
,目标 API 30 的 Foi 项目必须使用支持 ExifInterface:
implementation "androidx.exifinterface:exifinterface:1.3.2"
和带有 InputStream 的 ExifInterface 构造函数:
import androidx.exifinterface.media.ExifInterface
private fun calculateBitmapRotateDegrees(uri: Uri): Float {
var exif: ExifInterface? = null
try {
val inputStream: InputStream? = contentResolver.openInputStream(uri)
inputStream?.run {
exif = ExifInterface(this)
}
} catch (e: IOException) {
e.printStackTrace()
}
exif?.run {
val orientation = getAttributeInt(
ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL
)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90F
ExifInterface.ORIENTATION_ROTATE_180 -> 180F
ExifInterface.ORIENTATION_ROTATE_270 -> 270F
else -> 0F
}
}
return 0F
}