【问题标题】:How to get Exif data from an InputStream rather than a File?如何从 InputStream 而不是 File 中获取 Exif 数据?
【发布时间】:2017-01-25 05:21:42
【问题描述】:

之所以这么问是因为文件选择器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,但这对我来说似乎是一个糟糕的解决方法。

【问题讨论】:

    标签: android image file path exif


    【解决方案1】:

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

    示例: 首先将此行添加到您的 gradle 文件中:

    编译'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 LibraryExifInterface

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      相关资源
      最近更新 更多