android - 关于如何从 URI 获取 Exif 数据的最终答案

标签 android android-intent uri exif android-contentresolver

这里有很多问题讨论了这个主题,结果大多不同,并且由于 API 更改和不同类型的 URI,没有明确的答案

我自己没有答案,但让我们谈谈吧。 ExifInterface 有一个接受 filePath 的构造函数。这本身就很烦人,因为现在不鼓励依赖路径 - 您应该使用 Uris 和 ContentResolver。好的。

我们的名为 uriUri 可以从 onActivityResult 中的 Intent 中检索(如果您使用 ACTION_GET_CONTENT< 从图库中选择图片) 或者可以是我们之前拥有的 Uri(如果您从相机中选择图片并调用 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri))。

API<19

我们的 uri 可以有两种不同的模式:

  • 来自摄像头的 Uris 大多具有 file:// 架构。那些很容易治疗,因为他们掌握了道路。你可以调用 new ExifInterface(uri.getPath()) 就完成了。
  • 来自画廊或其他内容提供商的 Uris 通常具有 content:// 接口(interface)。我个人不知道那是怎么回事,但让我发疯了。

据我所知,第二种情况应该使用 ContentResolver 处理,您可以使用 Context.getContentResolver() 获得。在任何情况下,以下适用于我测试过的所有应用:

public static ExifInterface getPictureData(Context context, Uri uri) {
    String[] uriParts = uri.toString().split(":");
    String path = null;

    if (uriParts[0].equals("content")) {
        // we can use ContentResolver.
        // let’s query the DATA column which holds the path
        String col = MediaStore.Images.ImageColumns.DATA;
        Cursor c = context.getContentResolver().query(uri,
                new String[]{col},
                null, null, null);

        if (c != null && c.moveToFirst()) {
            path = c.getString(c.getColumnIndex(col));
            c.close();
            return new ExifInterface(path);
        }

    } else if (uriParts[0].equals("file")) {
        // it's easy to get the path
        path = uri.getEncodedPath();
        return new ExifInterface(path);
    }
    return null;
}

API19+

我的问题是从 Kitkat 开始的 content:// URI。 Kitkat 引入了 Storage Access Framework(参见 here)以及新的 Intent、ACTION_OPEN_DOCUMENT 和平台选择器。不过据说

On Android 4.4 and higher, you have the additional option of using the ACTION_OPEN_DOCUMENT intent, which displays a picker UI controlled by the system that allows the user to browse all files that other apps have made available. From this single UI, the user can pick a file from any of the supported apps.

ACTION_OPEN_DOCUMENT is not intended to be a replacement for ACTION_GET_CONTENT. The one you should use depends on the needs of your app.

所以为了简单起见,假设我们可以使用旧的 ACTION_GET_CONTENT:它会触发一个选择器对话框,您可以在其中选择一个图库应用。

但是,内容方法不再适用。有时它适用于 Kitkat,但从不适用,例如 Lollipop。我不知道到底发生了什么变化。

我已经搜索并尝试了很多;专门针对 Kitkat 采取的另一种方法是:

String wholeId = DocumentsContract.getDocumentId(uri);
String[] parts = wholeId.split(“:”);
String numberId = parts[1];

Cursor c = context.getContentResolver().query(
    // why external and not internal ?
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    new String[]{ col },
    MediaStore.Images.Media._ID + “=?”,
    new String[]{ numberId },
    null);

这有时有效,但有时无效。具体来说,当 wholeId 类似于 image:2839 时它可以工作,但当 wholeId 只是一个数字时显然会中断。

您可以使用系统选择器尝试此操作(即使用 ACTION_OPEN_DOCUMENT 触发图库):如果您从“Recents”中选择图像,它会起作用;如果您从“下载”中选择图像,它会中断。

那怎么办?!

直接的答案是您没有,您在较新版本的操作系统中找不到来自内容 uri 的文件路径。可以说,不是所有的内容uri都指向图片甚至文件。

这对我来说完全没问题,起初我努力避免这种情况。但是,如果我们不应该使用路径,我们应该如何使用 ExifInterface 类?

我不明白现代应用程序是如何做到这一点的 - 查找方向和元数据是您立即面临的问题,而 ContentResolver 不提供任何这种意义上的 API。你有 ContentResolver.openFileDescriptor() 和类似的东西,但没有读取元数据的 API(它确实在那个文件中)。可能有外部库从流中读取 Exif 内容,但我想知道解决这个问题的通用/平台方法。

我在谷歌的开源应用中搜索过类似的代码,但一无所获。

最佳答案

用一些示例代码扩展 alex.dorokhov 的答案。支持库是一个很好的方法。

build.gradle

dependencies {
...    
compile "com.android.support:exifinterface:25.0.1"
...
}

示例代码:

import android.support.media.ExifInterface;
...
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
      ExifInterface exif = new ExifInterface(inputStream);
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
      e.printStackTrace();
    }

一旦我们开始定位 api 25(也许 24+ 也有问题)但仍然支持回到 api 19,我必须这样做的原因,在 android 7 上,如果我将 URI 传递给只是引用文件的相机。因此,我必须创建一个 URI 以像这样传递给相机 Intent 。

FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", tempFile);

问题在于该文件无法将 URI 转换为真实的文件路径(除了保留临时文件路径)。

关于android - 关于如何从 URI 获取 Exif 数据的最终答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696787/

相关文章:

Android NDK 链接时间

android - 禁止使用多个vararg参数

安卓 : Can I use this intent from a 3rd party application?

java - 如何将 Fop 配置文件加载到 FopFactory

android - 如何使布局组件在android中响应?

android - RecyclerView 适合屏幕时不要折叠 Toolbar

android - 在 Activity 中嵌入 Google 日历

java - ListView 自定义布局的适配器

hyperlink - 如何为 Viber 制作深度链接,重定向到手机上的特定号码

android - 如何在 exoplayer 中播放本地媒体文件的硬编码内容 uri