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

我想添加GPS数据,如经度和纬度到JPEG照片。 通过标签卡(NFC)捕获照片在logcat中可以显示正确的值,但是这些值不能写入jpg照片文件!

下面是我的代码:它用于保存jpg文件并调用下面的方法该方法用于将EXIF GPS参数添加到jpg中GPS参数(如经度和纬度)已在另一个活动中使用。

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

IOexception的位置是否重要?

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

Solutions Collecting From Web of "Android将EXIF GPS纬度和经度写入JPEG失败"

好吧,我为此奋斗了很长一段时间,但终于明白了。 上次我使用它的代码工作:

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