Android将EXIF GPS纬度和经度写入JPEG失败

前端之家收集整理的这篇文章主要介绍了Android将EXIF GPS纬度和经度写入JPEG失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在jpeg照片中添加经度和纬度等GPS数据.
通过标签卡(NFC)捕获照片
在logcat中可以显示正确的值但是这些值不能写入jpg照片文件中!

以下是我的代码
它用于获取保存的jpg文件调用下面的方法
方法用于将EXIF GPS参数添加到jpg中
诸如经度和纬度的GPS参数已经在另一活动中被采用.

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

IO异常的位置是否重要?

以下是可能导致失败的重要日志cat日志:
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();

解决方法

好吧,我很长时间都在努力,但终于明白了.上次我使用它时这个代码工作:
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," + seconds + "/1";


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

              exif.saveAttributes();

          }

猜你在找的Android相关文章