Android ExifInterface无法保存属性

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),但不确定它是否能解决这个问题。
请问有人能够解释如何修复这个问题吗?
谢谢!

我遇到了相同的问题,我使用DocumentFile保存图像并使用DocumentFile.getUri()打开InputStream,但是当我尝试使用InputStream保存EXIF数据到图像时,我得到了相同消息的异常。我尝试保存到设备内存和SD卡,但两者都引起了异常。ExifInterface(InputStream)可能有漏洞吗? - Thracian
3个回答

6

使用输入流无法进行属性变异

您可以检查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 无法处理输入流的情况。 - Karan Harsh Wardhan

3
ExifInterface does not support saving attributes for the current input.

当前输入是一个 InputStream。不能将数据保存到 InputStream 中,只能保存至 OutputStream 中。
第二个问题是,assets 目录下的文件是只读的,因此即使尝试打开 OutputStream,也无法进行操作。因此此操作不可行。

0
我认为可能的问题是:您正在尝试向在应用程序打包期间放置在应用程序内部的只读资产添加属性。
而向zip文件中的文件添加属性仍然不受exifInterface支持。但是,您可以轻松地向其他存在于外部(例如SD卡)的文件添加属性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接