首页 > 解决方案 > 读取 Exif 数据 android 图像裁剪器时出错

问题描述

我使用这个图像裁剪库并根据需要裁剪图像,但我无法让它读取用于旋转的 exif 数据。尽管在清单文件中设置了读写外部权限,但我不断收到未找到文件的异常。这是我尝试读取 exif 数据的方式

public static int getExifRotation(File imageFile) {
    if (imageFile == null) return 0;
    try {
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        // We only recognize a subset of orientation tag values
        switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return 270;
            default:
                return ExifInterface.ORIENTATION_UNDEFINED;
        }
    } catch (IOException e) {
        Log.e("Error getting Exif data", e);
        return 0;
    }
}

这是 logcat 响应

E/android-crop: Error getting Exif data
java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/20180610_014252.jpg: open failed: EACCES (Permission denied)
    at libcore.io.IoBridge.open(IoBridge.java:452)
    at java.io.FileInputStream.<init>(FileInputStream.java:76)
    at java.io.FileInputStream.<init>(FileInputStream.java:103)
    at android.support.media.ExifInterface.<init>(ExifInterface.java:1300)
    at com.nextgen.cropping.CropUtil.getExifRotation(CropUtil.java:52)

我什至改变了

imageFile.getAbsolutePath()

imageFile.getPath()

它不起作用。CropUtil.java:52 指这一行

ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());

标签: androidfileorientationexif

解决方案


推荐阅读