【问题标题】:Gallery images taken on Samsung androids always in landscape even if taken in portrait即使是纵向拍摄,在三星机器人上拍摄的画廊图像也始终是横向的
【发布时间】:2015-08-19 08:44:48
【问题描述】:

使用以下代码从图库中挑选图片:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, PICK_FILE_RESULT_CODE);

使用以下代码获取结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_FILE_RESULT_CODE) {
        if (resultCode == RESULT_OK && data != null && data.getData() != null) {
            // Get the Uri of the selected file
            Uri uri = data.getData();

            //Using Picasso to load uri to imageView
            //Image is in landscape even if it was taken in portrait

        }
    }
}

该代码适用于 HTC 和 Nexus 手机,但仅适用于三星设备(Galaxy 5 和 Galaxy 5 mini),如果照片是纵向拍摄的,则方向错误。查看 ExifInterface 时,方向未定义..

File imageFile = new File(uri.getPath());
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
//orientation is always 0 for samsung devices = ORIENTATION_UNDEFINED

我怎样才能正确地呈现图像或者确定正确的方向以便我可以旋转图像?

【问题讨论】:

  • 向我发送 2 张示例图片(每个方向 1 张),如果我找到任何定义方向的信息,我会告诉你 (bitbank@pobox.com)。
  • 我已经发送了一封包含图片的电子邮件,感谢您抽出宝贵的时间! @BitBank
  • 图像确实包含 EXIF 方向信息。第一个是“左90度”,第二个是“180度”。
  • 我之前没有尝试过Android ExifInterface 类,但如果你觉得它不够用,你可能会发现metadata-extractor 库很有用。对于快速测试,Phil Harvey 的出色 Exiftool 非常出色。
  • 感谢@BitBank 的帮助,我找到了方向,看看我的回答

标签: android orientation exif samsung-mobile android-gallery


【解决方案1】:

我能够使用以下代码获得方向:

public static int getExifOrientation(Context context, Uri uri) {
    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = null;
    try {
        String id = DocumentsContract.getDocumentId(uri);
        id = id.split(":")[1];
        cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                CONTENT_ORIENTATION, MediaStore.Images.Media._ID + " = ?", new String[] { id }, null);
        if (cursor == null || !cursor.moveToFirst()) {
            return 0;
        }
        return cursor.getInt(0);
    } catch (RuntimeException ignored) {
        // If the orientation column doesn't exist, assume no rotation.
        return 0;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

https://vikaskanani.wordpress.com/2011/07/17/android-re-size-image-without-loosing-exif-information/

【讨论】:

  • 这里的 CONTENT_ORIENTATION 是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 2017-03-03
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多