【问题标题】:Android ExifInterface not saving attributeAndroid ExifInterface 不保存属性
【发布时间】:2017-05-25 23:53:09
【问题描述】:

下面是我的代码-

try {
  InputStream inputStream = getAssets().open("thumbnail.jpg");
  exifInterface = new ExifInterface(inputStream);
  exifInterface.setAttribute(ExifInterface.TAG_ARTIST,"TEST INPUT");
  exifInterface.saveAttributes();
} catch (IOException e) {
  e.printStackTrace();
}

exifInterface.saveAttributes() 行我收到以下错误 -

java.io.IOException: ExifInterface 不支持保存属性 用于当前输入。

我不确定错误是由于图像文件还是由于属性。我正在努力挽救。我也在网上寻找可能的解决方案(例如 Sanselan),但不确定它是否能解决这个问题。

谁能解释一下如何解决这个问题?

谢谢!

【问题讨论】:

  • 我遇到了同样的问题,我使用 DocumentFile 保存图像并使用 DocumentFile.getUri() 打开 InputStream 但是当我尝试使用 InputStream 将 EXIF 数据保存到图像时,我得到相同的异常消息.我尝试保存到设备内存和 SD 卡,但都导致异常。 ExifInterface(InputStream) 可以被窃听吗?

标签: android android-exifinterface


【解决方案1】:

您不能使用输入流进行属性突变

你可以查看ExifInterface的代码,上面写着:

/**
     * Reads Exif tags from the specified image input stream. Attribute mutation is not supported
     * for input streams. The given input stream will proceed its current position. Developers
     * should close the input stream after use. This constructor is not intended to be used with
     * an input stream that performs any networking operations.
     */
    public ExifInterface(InputStream inputStream) throws IOException {
   /* Irrelevant code here */

所以,如果你想写入文件的元数据,你需要在构造函数中传递文件。否则它将失败。您还可以在类中看到总是失败的代码(使用 InputStream):

public void saveAttributes() throws IOException {
        if (!mIsSupportedFile || mMimeType != IMAGE_TYPE_JPEG) {
            throw new IOException("ExifInterface only supports saving attributes on JPEG formats.");
        }
        if (mFilename == null) {
            throw new IOException(
                    "ExifInterface does not support saving attributes for the current input.");
        }

 //Irrelevant code

因此,使用 ExifInterface(file),您将能够使您的代码正常工作。

编码愉快!

【讨论】:

  • 感谢深入研究,没想到 exifinterface 不能处理输入流的情况
【解决方案2】:
ExifInterface does not support saving attributes for the current input.

当前输入是InputStream。无法将数据保存到InputStream。仅限OutputStream

第二个问题是assets 中的文件是只读的。因此,如果您尝试过,您甚至无法打开OutputStream。太不可能了。

【讨论】:

    【解决方案3】:

    我认为可能存在的问题是:您正在尝试将属性添加到在创建应用程序的 zip 期间放置在应用程序内的只读资产。

    exifInterface 仍然不支持向 zip 中的文件添加属性。但是,您可以轻松地将属性添加到 SDCard 之外存在的其他文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      相关资源
      最近更新 更多