安卓无法将EXIF GPS纬度和经度信息写入JPEG文件中。

9
我想将GPS数据,如经度和纬度,添加到JPEG照片中。 该照片是通过点击卡片(NFC)进行捕获的。 在logcat中可以显示正确的值,但这些值无法写入jpg照片文件!
以下是我的代码: 它用于获取保存的jpg文件并调用下面的方法 该方法用于将EXIF GPS参数添加到jpg中 GPS参数,如经度和纬度,已在另一个活动中获取。
我使用Firefox中的EXIF Viewer查看结果。
IO异常的位置是否重要?
以下是可能导致失败的重要logcat日志: 07-26 11:48:30.386:D / NativeNfcTag(195):标签丢失,重新启动轮询循环
 public static void writeFile (File photo, double latitude, double longitude) throws IOException{


    ExifInterface exif = null;

    try{
        Log.v("latiDouble", ""+latitude);
        Log.v("longiDouble", ""+longitude);
        exif = new ExifInterface(photo.getCanonicalPath());
        if (exif != null) {
        double latitu = latitude;
        double longitu = longitude;
        double alat = Math.abs(latitu);
        double along = Math.abs(longitu);
        String stringLati = convertDoubleIntoDegree(alat);
        String stringLongi = convertDoubleIntoDegree(along);
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, stringLati);            
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, stringLongi);
        Log.v("latiString", ""+ stringLati);
        Log.v("longiString", ""+ stringLongi);
        exif.saveAttributes();
        String lati = exif.getAttribute (ExifInterface.TAG_GPS_LATITUDE);  
         String longi = exif.getAttribute (ExifInterface.TAG_GPS_LONGITUDE);  
         Log.v("latiResult", ""+ lati);
         Log.v("longiResult", ""+ longi);

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

}

以下是调用方法的位置:
    Cursor locationCursor = dbHandler.fetchGpsLocationTypeByAttendInfoID(attendInfoId);
      if (locationCursor.getCount()>0) {
        double latitude = dbHandler.fetchDoubleItem(locationCursor,"LATITUDE");
        double longitude = dbHandler.fetchDoubleItem(locationCursor,"LONGITUDE");


            Log.v("latitude",""+latitude);
            Log.v("latitude",""+longitude);     

            try {
                GpsUtils.writeFile(photoFile, latitude, longitude);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
    dbHandler.close();

    cameraHandler.startPreview();

我们可能需要更多关于你的代码的信息(逐行解释并说明每一行的作用可能会帮助你找到解决方案),以及你已经为这个问题做了哪些研究。例如,如果你遇到了异常,那么是什么异常?基本上,如果你认为有什么可以帮助回答问题的东西,那么添加它会很好。 - user457812
我所看到的是参数设置有效,并且可以从logcat中显示,但是当使用Firefox的EXIF Viewer打开图像时,我无法看到任何在此图片中写入的GPS数据。 - Jeff Bootsholz
1个回答

14

好的,我花了很长时间才解决了这个问题,但最终还是搞定了。上次使用时,这段代码是起作用的:

ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());
              //String latitudeStr = "90/1,12/1,30/1";
              double lat = location.getLatitude();
              double alat = Math.abs(lat);
              String dms = Location.convert(alat, Location.FORMAT_SECONDS);
              String[] splits = dms.split(":");
              String[] secnds = (splits[2]).split("\\.");
              String seconds;
              if(secnds.length==0)
              {
                  seconds = splits[2];
              }
              else
              {
                  seconds = secnds[0];
              }

              String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
              exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitudeStr);

              exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat>0?"N":"S");

              double lon = location.getLongitude();
              double alon = Math.abs(lon);


              dms = Location.convert(alon, Location.FORMAT_SECONDS);
              splits = dms.split(":");
              secnds = (splits[2]).split("\\.");

              if(secnds.length==0)
              {
                  seconds = splits[2];
              }
              else
              {
                  seconds = secnds[0];
              }
              String longitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";


              exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitudeStr);
              exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon>0?"E":"W");

              exif.saveAttributes();

          }

你完成后是否重新保存了文件?这段代码对我有效,EXIF查看器可以看到嵌入的位置——我刚刚测试过了。 - Kaediil
当我使用https://maps.google.com/并输入坐标22.3071491,114.1719491时,它是有效的。 - Jeff Bootsholz
抱歉,我不知道问题出在哪里,但我知道它应该可以工作,而且对我来说是可以的。 - Kaediil
当我使用 FxiF 时,GPS 数据的插入是成功的。 - Jeff Bootsholz
我需要实现以下修复:splits[0] + "/1," + splits[1] + "/1," + seconds.replace(',','.') + "/1"; - Benjamin Toueg
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接