如何将GPS坐标写入Android上的EXIF数据 [英] How to write GPS coordinates to EXIF data on Android

查看:188
本文介绍了如何将GPS坐标写入Android上的EXIF数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写地理标记应用程序,用户从照片库中选择图像,然后该图像通过其EXIF文件获取当前GPS坐标。



到目前为止,我可以拉起画廊,选择图像,并找到我当前的GPS坐标,但我无法将这些坐标写入EXIF文件。每当我选择一个图像,我都可以查看它,但没有数据写入EXIF文件。

我查阅了几个如何写入EXIF的例子,并且我的代码看起来是正确的(至少对我来说)。任何人都可以帮我弄清楚为什么我不写任何数据?

这是我的代码:

  //将GPS线插入exif文件
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();
}

//将十进制度数转换为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而不是文件名。这可能与我的问题有关吗?

解决方案

解决方案。对文件路径进行硬编码是个问题。我使用了代码


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

获得 picturePath 然后用于

  ExifInterface exif = new ExifInterface(picturePath); 


I'm trying to write a geotagging app where the user selects an image form the photo gallery, and then that image gets the current GPS coordinates written to it through its EXIF file.

So far I am able to pull up the gallery, select an image, and find my current GPS coordinates, but I cannot write those coordinates to the EXIF file. Whenever I do select an image, I can view it, but no data is written to the EXIF file.

I have looked up several examples for how to write to EXIF, and my code looks correct (to me at least). Can anyone help me figure out why I am not writing any data?

Here's my code:

//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;
    }

Thanks for the help!

Edit: At this time I was trying to hardcode the file path and name into ExifInterface, so that's why it says "/sdcard/dcim/100MEDIA/IMAG0020.jpg"instead of filename. Could that have anything to do with my problem?

解决方案

Solved it. Hardcoding the file path was the problem. I used the code

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

to obtain picturePath which was then used in

ExifInterface exif = new ExifInterface(picturePath);

这篇关于如何将GPS坐标写入Android上的EXIF数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆