如何在android 10中获取图像的方向信息?

问题描述 投票:0回答:2

自android 10起,在访问媒体文件方面进行了一些更改。在查阅了文档https://developer.android.com/training/data-storage/shared/media之后,我已经能够将媒体内容加载到位图中,但是我没有获得方向信息。我知道对图像的位置信息有一些限制,但是这些exif限制也会影响方向信息吗?如果还有其他方法可以获取图像的方向信息,请告诉我。正在使用的代码如下(始终返回0-未定义的值)。谢谢。

ContentResolver resolver = getApplicationContext().getContentResolver();
try (InputStream stream = resolver.openInputStream(selectedFileUri)) {
 loadedBitmap = BitmapFactory.decodeStream(stream);
 ExifInterface exif = new ExifInterface(stream);
 orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
java android exif mediastore android-10.0
2个回答
0
投票

BitmapFactory.decodeStream消耗了整个流并关闭了它。

您应先打开一个新的流,然后再尝试阅读该exif。


0
投票

首先,请考虑在不同SDK版本中使用的API,请使用AndroidX ExifInterface Library

第二,ExifInterface用于读写各种图像文件格式的Exif标签。支持读取:JPEG,PNG,WebP,HEIF,DNG,CR2,NEF,NRW,ARW,RW2,ORF,PEF,SRW,RAF。

但是您将其用于位图,位图没有任何EXIF标头。从位图加载位图时,您已经丢弃了所有EXIF数据。在数据的原始来源而不是位图上使用ExifInterface]

您可以尝试使用以下代码获取信息,并且请记住使用原始流。] >>

public static int getExifRotation(Context context, Uri imageUri) throws IOException {
    if (imageUri == null) return 0;
    InputStream inputStream = null;
    try {
        inputStream = context.getContentResolver().openInputStream(imageUri);
        ExifInterface exifInterface = new ExifInterface(inputStream);
        int orienttation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
        switch (orienttation) {
            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;
        }
    }finally {
       //here to close the inputstream
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.