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):标记丢失,重启轮询循环

  1. public static void writeFile (File photo,double latitude,double longitude) throws IOException{
  2.  
  3.  
  4. ExifInterface exif = null;
  5.  
  6. try{
  7. Log.v("latiDouble",""+latitude);
  8. Log.v("longiDouble",""+longitude);
  9. exif = new ExifInterface(photo.getCanonicalPath());
  10. if (exif != null) {
  11. double latitu = latitude;
  12. double longitu = longitude;
  13. double alat = Math.abs(latitu);
  14. double along = Math.abs(longitu);
  15. String stringLati = convertDoubleIntoDegree(alat);
  16. String stringLongi = convertDoubleIntoDegree(along);
  17. exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,stringLati);
  18. exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,stringLongi);
  19. Log.v("latiString",""+ stringLati);
  20. Log.v("longiString",""+ stringLongi);
  21. exif.saveAttributes();
  22. String lati = exif.getAttribute (ExifInterface.TAG_GPS_LATITUDE);
  23. String longi = exif.getAttribute (ExifInterface.TAG_GPS_LONGITUDE);
  24. Log.v("latiResult",""+ lati);
  25. Log.v("longiResult",""+ longi);
  26.  
  27. }
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31.  
  32. }
  33.  
  34. }

以下是调用方法的位置

  1. Cursor locationCursor = dbHandler.fetchGpsLocationTypeByAttendInfoID(attendInfoId);
  2. if (locationCursor.getCount()>0) {
  3. double latitude = dbHandler.fetchDoubleItem(locationCursor,"LATITUDE");
  4. double longitude = dbHandler.fetchDoubleItem(locationCursor,"LONGITUDE");
  5.  
  6.  
  7. Log.v("latitude",""+latitude);
  8. Log.v("latitude",""+longitude);
  9.  
  10. try {
  11. GpsUtils.writeFile(photoFile,latitude,longitude);
  12. } catch (IOException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16.  
  17. }
  18. dbHandler.close();
  19.  
  20. cameraHandler.startPreview();

解决方法

好吧,我很长时间都在努力,但终于明白了.上次我使用它时这个代码工作:
  1. ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());
  2. //String latitudeStr = "90/1,12/1,30/1";
  3. double lat = location.getLatitude();
  4. double alat = Math.abs(lat);
  5. String dms = Location.convert(alat,Location.FORMAT_SECONDS);
  6. String[] splits = dms.split(":");
  7. String[] secnds = (splits[2]).split("\\.");
  8. String seconds;
  9. if(secnds.length==0)
  10. {
  11. seconds = splits[2];
  12. }
  13. else
  14. {
  15. seconds = secnds[0];
  16. }
  17.  
  18. String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
  19. exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,latitudeStr);
  20.  
  21. exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,lat>0?"N":"S");
  22.  
  23. double lon = location.getLongitude();
  24. double alon = Math.abs(lon);
  25.  
  26.  
  27. dms = Location.convert(alon,Location.FORMAT_SECONDS);
  28. splits = dms.split(":");
  29. secnds = (splits[2]).split("\\.");
  30.  
  31. if(secnds.length==0)
  32. {
  33. seconds = splits[2];
  34. }
  35. else
  36. {
  37. seconds = secnds[0];
  38. }
  39. String longitudeStr = splits[0] + "/1," + seconds + "/1";
  40.  
  41.  
  42. exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,longitudeStr);
  43. exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,lon>0?"E":"W");
  44.  
  45. exif.saveAttributes();
  46.  
  47. }

猜你在找的Android相关文章