Android 11从EXIF获取图像方向

Android 11从EXIF获取图像方向,android,filenotfoundexception,permission-denied,android-10.0,android-exifinterface,Android,Filenotfoundexception,Permission Denied,Android 10.0,Android Exifinterface,您好,我有一个应用程序,它有CompileSDK版本30和TargetSDK版本30。 因为我需要知道图像的方向,所以我写了以下内容: val exif = ExifInterface(imageFile.absolutePath) val orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIE

您好,我有一个应用程序,它有CompileSDK版本30和TargetSDK版本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)
java.io,FileNotFoundException:/storage/simulated/0/DCIM/Camera/xxx.jpg:open失败的每次访问(权限被拒绝)
...
位于android.media.ExifInterface.(ExifInterface.java.1389)

我想做的是得到图像,并知道它的方向,但我不能在互联网上找到任何样本。有人能给我一个提示吗?谢谢

在Android 11上,您可以访问该摄像头目录,但大多数情况下不能访问该目录中属于其他应用程序的文件

如果使用经典的文件系统路径,则不会

那么,哪个应用程序将这些文件放在那里


如果不是,您可以使用ACTION_OPEN_DOCUMENT之类的操作,让用户选择一个可以为您提供良好uri的文件。

在Android 11上,您可以访问该摄像头目录,但大多数情况下不能访问该目录中属于其他应用程序的文件

如果使用经典的文件系统路径,则不会

那么,哪个应用程序将这些文件放在那里


如果不是,您可以使用ACTION_OPEN_DOCUMENT之类的操作,让用户选择一个可以为您提供良好uri的文件。

Foi项目与目标API 30必须使用support 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
}

目标API为30的Foi项目必须使用support 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
}

您从哪里获得
imageFile
?请包含生成该变量的代码。将完整的类代码张贴在此处。您从何处获得
imageFile
?请包含生成该变量的代码。请在此处发布完整的类代码