【问题标题】:Getting rotation information from Content:// and File:// URIs从 Content:// 和 File:// URI 获取轮换信息
【发布时间】:2017-08-13 02:23:42
【问题描述】:

我在获取用户选择的图片然后确定是否需要旋转图片时遇到问题。我想我会很聪明,让我的所有处理都在 Uris 上运行,以便不知道图像的确切来源,但我看到的确定旋转的方法似乎需要 Exif 信息,而这些信息不适用于 Content: // Uri 的类型。

问题:如果我的图像句柄是 Uri,而您不确定该图像的来源,我如何确定该图像是否需要旋转?

我这样做是为了获取图像:

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, INTENT_IMAGE_LOAD);

...在 onActivityResult() 中,我正在接收 Uri 以供以后处理。

        Uri imageUri = data.getData();

        // if the imageUri needs rotation, then fix it here (I know how to fix it).
        // ... but I think I need to use Exif data which I can't get from Content:// URI
        // hmmmmmmmm


        doMyProcessingOnUri(context, imageUri);

显然,Android 7.0 已经改变了使用此类 Intent 的某些方面,我认为已发布的大部分帮助都与 7.0 之前的版本有关。

【问题讨论】:

  • 理想情况下,我的处理能够处理来自画廊、相机(最终)甚至我自己的项目资源(如 Drawables 和 Asset/ 或 Raw/)的位图。据我所知,所有这些都可以制成位图,并且都可以表示为 Uri——这就是为什么 Uri 似乎是处理图像的一个很好的共同点。

标签: android android-intent android-image


【解决方案1】:

根据Introducing the ExifInterface Support Library

对于从具有 content:// URI 的其他应用程序接收图像的应用程序(例如由以 API 24 或更高版本为目标的应用程序发送的图像),ExifInterface 现在直接在 InputStream 之外工作;这使您可以轻松地直接从收到的 content:// URI 中提取 Exif 信息,而无需创建临时文件。

他们继续展示如何准确提取旋转信息:

InputStream in = getContentResolver().openInputStream(uri);
ExifInterface exifInterface = new ExifInterface(in);
int rotation = 0;
int orientation = exifInterface.getAttributeInt(
    ExifInterface.TAG_ORIENTATION,
    ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
  case ExifInterface.ORIENTATION_ROTATE_90:
    rotation = 90;
    break;
  case ExifInterface.ORIENTATION_ROTATE_180:
    rotation = 180;
    break;
  case ExifInterface.ORIENTATION_ROTATE_270:
    rotation = 270;
    break;
}

您可以使用以下 Gradle 导入来包含 ExifInterface 支持库:

compile "com.android.support:exifinterface:26.0.1"

【讨论】:

  • 哇。这看起来令人鼓舞,我以前没有看到这一点。谢谢,我会检查一下。
  • 模拟器可以吗?这总是在模拟器 android 中返回 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
  • 2018-08-30
相关资源
最近更新 更多