【问题标题】:Android ExifInterface saveAttributes() Without Overwriting Previous Data?Android ExifInterface saveAttributes() 不覆盖以前的数据?
【发布时间】:2014-10-21 09:39:51
【问题描述】:

我正在尝试将 lat-long 和其他数据写入我的自定义相机应用中的 jpeg 的 Exif 标头。通常,android 会自动使用光圈、ISO、快门速度等数据填充标题。但是,当我手动添加创建 ExifInterface 实例时,使用 SetAttributes() 设置 GPS 位置,然后调用 SaveAttributes();所有其他相机数据都消失了。

这应该发生吗?我怎样才能简单地添加标签而不覆盖其他所有内容?

我在其他地方看到了一个创建两个 ExifInterfaces 的示例,一个旧的(从图片中)和一个新的,并将每个填充的值从旧的复制到新的值以及任何其他数据。然而,这既烦人又冗长。我想找到更好的解决方案。

这是我的代码:

try{ 
    ExifInterface exif = new ExifInterface(pictureFile.getAbsolutePath());
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, mGpsLocation.getLatDms());
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, mGpsLocation.getLonDms());
    exif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE, mGpsLocation.getAltDms());
    exif.saveAttributes(); 
} catch(IOException e){
    e.printStackTrace(); 
}

谢谢

【问题讨论】:

  • 我不确定你为什么会这样。我已经尝试过您提到的相同方法,并且我的 EXIF 数据都没有被覆盖。保存属性后仅修改我的 GPS 位置。在以下设备上测试,Nexus Prime - 4.2.1 Motorola Moto E - 4.4.4

标签: android android-camera jpeg exif


【解决方案1】:

从我看到的文档来看,这是不应该发生的。 http://developer.android.com/reference/android/media/ExifInterface.html

public void saveAttributes ()

Added in API level 5
Save the tag data into the JPEG file. This is expensive because it involves copying all the JPG data from one file to another and deleting the old file and renaming the other. It's best to use setAttribute(String, String) to set all attributes to write and make a single call rather than multiple calls for each attribute.

Throws
IOException 

它清楚地表明它正在复制所有数据,包括你所说的正在消失的东西。您能否发布您正在测试它的内容。如果您在 Android Studio 上,您可以尝试 File -> Invalidate Caches / Restart -> Invalidate and Restart。

另外,我在这里找到了一个关于丢失数据的类似问题的答案: https://stackoverflow.com/a/13784057/3585278

正如其他人所指出的,您必须从 原始图像到最终调整大小的图像。三色兰安卓 图书馆通常最适合这个。根据 Android 操作系统版本, ExifInterface 有时会破坏 EXIF 数据。除此之外 ExifInterface 还处理有限数量的 Exif 标签——即 只有它“知道”的标签。另一方面,三色兰将 保留所有 EXIF 标签和标记注释。 这是一篇博文,展示了如何使用 Sanselan 复制图像数据: http://bricolsoftconsulting.com/copying-exif-metadata-using-sanselan/ 顺便说一句,在 Android 上,我也倾向于旋转图像并删除 Orientation EXIF 标签。例如,在装有 Android 4.03 的 Nexus S 上, 相机在 EXIF 元数据中设置了方向标签,但是 webview 忽略了该信息并错误地显示了图像。 可悲的是,旋转实际图像数据并删除 EXIF 方向标签是让每个程序都显示图像的唯一方法 正确。

我希望这会有所帮助。

【讨论】:

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