无法使用EXIF或MediaStore

时间:2016-09-04 13:13:44

标签: android bitmap exif

这是我尝试获取方向的一张示例图片的详细信息: image with orientation 270

使用EXIF我总是得到ORIENTATION_UNDEFINED(= 0)。这是代码:

ExifInterface exif = new ExifInterface(imageUri.getPath());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

所以,我搜索并发现了许多类似的问题。建议使用MediaStore中的图像细节。我是这样做的:

String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
try {
    Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null);
    if (cursor.moveToFirst()) {
        orientation = cursor.getInt(0);
    }
    cursor.close();
} catch (Exception e) {}

以上代码也没有给出任何结果," cursor"变为空!

有方向我会做图像旋转和东西。我该怎么办才能拍照?

1 个答案:

答案 0 :(得分:3)

只有imageUri具有file方案时,您的第一段代码才有效。很多时候,它不会。

只有当imageUri是来自Uri的{​​{1}}时,您的第二段代码才有效。

相反,请使用an ExifInterface implementation that works with streams,以便您可以使用MediaStoreContentResolver来获取方向。这应适用于所有openInputStream()值,但JPEG是否包含方向标题会因图像而异。

This sample app演示了这种方法。在我的情况下,图片位于Uri,因此我的assets/来自InputStream,而不是AssetManager,但除此之外它应该是相同的:

ContentResolver

(其中 ExifInterface exif=new ExifInterface(); exif.readExif(is, ExifInterface.Options.OPTION_ALL); ExifTag tag=exif.getTag(ExifInterface.TAG_ORIENTATION); int orientation=(tag==null ? -1 : tag.getValueAsInt(-1)); is

相关问题