android - 如何从 InputStream 而不是 File 获取 Exif 数据?

标签 android image file path exif

我问这个的原因是因为文件选择器 Intent 的回调返回一个 Uri

通过 Intent 打开文件选择器:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), CHOOSE_IMAGE_REQUEST);

回调:

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CHOOSE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {

        if (data == null) {
            // Error
            return;
        }

        Uri fileUri = data.getData();
        InputStream in = getContentResolver().openInputStream(fileUri);

        // How to determine image orientation through Exif data here?
    }
}

一种方法是将 InputStream 写入实际的 File,但这对我来说似乎是一个糟糕的解决方法。

最佳答案

引入 25.1.0 支持库后,现在可以通过 InputStream 从 URI 内容(content://或 file://)读取 exif 数据。

例子: 首先将这一行添加到您的 gradle 文件中:

compile 'com.android.support:exifinterface:25.1.0'

Uri uri; // the URI you've received from the other app
InputStream in;
try {
  in = getContentResolver().openInputStream(uri);
  ExifInterface exifInterface = new ExifInterface(in);
  // Now you can extract any Exif tag you want
  // Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
  // Handle any errors
} finally {
  if (in != null) {
    try {
      in.close();
    } catch (IOException ignored) {}
  }
}

更多信息请查看:Introducing the ExifInterface Support Library , ExifInterface .

关于android - 如何从 InputStream 而不是 File 获取 Exif 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540646/

相关文章:

Jquery照片比较

windows - "descript.ion"文件规范?

android - 如何将 .o 文件添加到 Android ndk 构建

android - 在 imageview 中剪切图像

android - Volley - 在同一个 Activity android 中有多个请求

android - 如何使用 ant 脚本构建不同的 Android 应用程序

iphone - 在 iPhone 的 UIImageView 中显示图像

ios - 通过本地 URL 从图库加载图像(Swift)

c - 如果 .txt 文件中存在某个单词,则将该单词复制到另一个 txt 文件

java - 为什么 'File.exists' 返回 true,即使 NIO 'Files.exists' 类中的 'Files' 返回 false