今天主要给大家分享一个知识点——如何利用Exif为图片文件添加GPS坐标信息,关于这个知识点网上的资料很多,而且这个知识点本身应该是不难的,但是当我在项目使用时却遇到了一个问题:在读取某个图片的坐标信息时,发现与之前写入的坐标信息有很大的差异?下面让我们来分析一下这个问题吧:
首先,我们要了解什么是 ExifExifInterface这个类来添加或读取某个文件的 Exif信息。ExifInterface的使用方法非常简单,具体代码如下所示:
/**
* add exif info for file
*
* @param filePath the path of file
* @param location the location info
*/
public static void addExif(String filePath, Location location) {
try {
ExifInterface exif = new ExifInterface(filePath);
if (location != null) {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, location.getLongitude());
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() > 0.0f ? "E" : "W");
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, location.g