Android将EXIF GPS纬度和经度写入JPEG失败 [英] Android write EXIF GPS Latitude and Longitude onto JPEG failed

查看:21
本文介绍了Android将EXIF GPS纬度和经度写入JPEG失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将经度和纬度等 GPS 数据添加到 jpeg 照片中.照片是通过标签卡(NFC)拍摄的在 logcat 中可以显示正确的值,但这些值无法写入 jpg 照片文件!

I would like to add GPS data such as longitude and latitude into the jpeg photo. The photo is captured by tabbing the card (NFC) In the logcat correct values can be displayed but these values cannot be written into the jpg photo file !

以下是我的代码:用于获取保存的jpg文件并调用下面的方法该方法用于将EXIF GPS参数添加到jpg中经纬度等 GPS 参数已在其他活动中获取.

Below is my code: It is used to take saved jpg files and call the method below The method is used to add EXIF GPS parameters into the jpg The GPS parameters such as longitude and latitude are already taken in another activity.

我在 Firefox 中使用 EXIF 查看器来查看结果.

I use EXIF Viewer in Firefox to see the result.

IO Exception 的位置重要吗?

Does the position for the IO Exception matters?

以下是可能导致失败的重要日志猫日志:07-26 11:48:30.386: D/NativeNfcTag(195): 标记丢失,重新启动轮询循环

The following is the important log cat log which may render the failure: 07-26 11:48:30.386: D/NativeNfcTag(195): Tag lost, restarting polling loop

 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();

    }

}

下面是调用方法的位置...

The below is the position to call the method ...

    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();

推荐答案

好吧,我纠结了很久,终于搞定了.上次我使用它时,这段代码有效:

Ok, I struggled with this for a long time but finally got it. Last time I used it this code worked:

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();

          }

这篇关于Android将EXIF GPS纬度和经度写入JPEG失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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