如何在Android上将GPS坐标写入EXIF数据

时间:2013-05-03 01:00:44

标签: geolocation gps jpeg exif

我正在尝试编写一个地理标记应用程序,用户从照片库中选择一个图像,然后该图像通过其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。这与我的问题有什么关系吗?

1 个答案:

答案 0 :(得分:0)

解决了它。硬编码文件路径是问题所在。我用了代码

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);

获取picturePath,然后在

中使用
ExifInterface exif = new ExifInterface(picturePath);