3

下面是我的代码-

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),但不确定它是否能解决这个问题。

有人可以解释如何解决这个问题吗?

谢谢!

4

3 回答 3

5

您不能使用 Input Stream 进行属性突变

你可以查看 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),您将能够使您的代码工作。

快乐编码!

于 2017-12-28T20:56:46.860 回答
2
ExifInterface does not support saving attributes for the current input.

当前输入是一个InputStream. 无法将数据保存到InputStream. 只对一个OutputStream.

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

于 2017-01-11T09:40:11.283 回答
0

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

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

于 2017-01-11T09:22:17.557 回答