Android ExifInterface不保存属性

use*_*549 3 android android-exifinterface

下面是我的代码-

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();
}
Run Code Online (Sandbox Code Playgroud)

我在线上exifInterface.saveAttributes()收到以下错误 -

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

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

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

谢谢!

Lea*_*ira 6

您不能使用 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 */
Run Code Online (Sandbox Code Playgroud)

因此,如果您想写入文件的元数据,则需要在构造函数中传递该文件。否则就会失败。您还可以在类中看到总是失败的代码(使用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
Run Code Online (Sandbox Code Playgroud)

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

快乐编码!