在 Android 中将 EXIF 标签(属性)写入并保存到 JPEG 图像

Writing and saving EXIF tags (attributes) to JPEG image in Android

我已经设法使用 ExifInterface 从我的 phone 上的 JPEG 中读取 EXIF tags/attributes(元数据),显然我也可以 设置 属性和 save 属性。奇怪的是,如果我对图像文件进行设置+保存,我的应用程序能够获取属性并显示它。我还可以在另一个应用程序(Google Play 上的照片编辑器)中验证 EXIF 数据确实已写入。

ExifInterface exif = new ExifInterface(path_to_image); 
String x = exif.getAttribute("UserComment"); // here, x is always null...

exif.setAttribute("UserComment", "testtest");
exif.saveAttributes();

x = exif.getAttribute("UserComment");  // x = "testtest"

现在,EXIF 已保存到 JPEG 文件:see screenshot of my app。 照片编辑器应用程序也验证了这一点:see screenshot of that

但是,如果我注释掉 set+save 并只是获取(在与上面相同的图像上),我的应用程序无法 get/see 属性:

ExifInterface exif = new ExifInterface(path_to_image); 
String x = exif.getAttribute("UserComment");  // x = null (although we know it isn't)

所以:由于照片编辑器应用程序可以读取数据,所以我做错了(writing/saving)。另外,如果我重新 运行 set+save 相同的图像文件,我的应用程序会复制相同的标签!除了简单的设置+保存,然后获取,还有什么比这更重要的吗?

Update: It seems the problem is device-dependent. Although the UserComment does not appear to be among the tags explicitly supported by ExifInterface, certain devices are nevertheless able to set and get the value in the tag. It works on Nexus'es, but not on my Sony Xperia. Please take a look at my code for getting (query) and setting (update) the UserComment tag

解决方案似乎是使用ExifInterface。我要试试https://github.com/sephiroth74/Android-Exif-Extended

更新:更好的是,使用 Apache Sanselan,https://commons.apache.org/proper/commons-imaging/javadocs/api-release/org/apache/sanselan/Sanselan.html

你的代码对我有用。我认为您有一些简单的错误,例如您混淆了正在修改的文件和正在照片编辑器中查看的文件。