【发布时间】:2013-05-03 01:00:43
【问题描述】:
我正在尝试编写一个地理标记应用程序,用户从照片库中选择一个图像,然后该图像通过其 EXIF 文件获取当前的 GPS 坐标。
到目前为止,我能够调出图库、选择图像并找到我当前的 GPS 坐标,但我无法将这些坐标写入 EXIF 文件。每当我选择图像时,我都可以查看它,但没有数据写入 EXIF 文件。
我查看了几个关于如何写入 EXIF 的示例,并且我的代码看起来是正确的(至少对我而言)。谁能帮我弄清楚为什么我不写任何数据?
这是我的代码:
//place GPS cords into exif file
try {
ExifInterface exif = new ExifInterface("/sdcard/dcim/100MEDIA/IMAG0020.jpg");
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, DMSconv(lat));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,DMSconv(lon));
if (lat > 0)
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
else
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
if (lon > 0)
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
else
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
//convert from decimal degrees to DMS
String DMSconv(double coord) {
coord = (coord > 0) ? coord : (-1)*coord; // -105.9876543 -> 105.9876543
String sOut = Integer.toString((int)coord) + "/1,"; // 105/1,
coord = (coord % 1) * 60; // .987654321 * 60 = 59.259258
sOut = sOut + Integer.toString((int)coord) + "/1,"; // 105/1,59/1,
coord = (coord % 1) * 6000; // .259258 * 6000 = 1555
sOut = sOut + Integer.toString((int)coord) + "/1000"; // 105/1,59/1,15555/1000
return sOut;
}
感谢您的帮助!
编辑:此时我试图将文件路径和名称硬编码到 ExifInterface 中,这就是为什么它说"/sdcard/dcim/100MEDIA/IMAG0020.jpg"而不是filename。这和我的问题有关系吗?
【问题讨论】:
-
解决了。硬编码文件路径是问题所在。我使用了代码
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex);并使用了picturePath。
标签: geolocation gps jpeg exif