2012-07-25 100 views
7

我想添加GPS數據,如經度和緯度到JPEG照片。 通過標籤卡(NFC)捕獲照片 在logcat中可以顯示正確的值,但這些值不能寫入jpg照片文件!Android寫入EXIF GPS經緯度到JPEG失敗

下面是我的代碼: 它是用來取保存JPG文件並調用下面 的方法,該方法被用來添加EXIF GPS參數轉換成JPG 的GPS參數,如經度和緯度都已經採取的另一活動。

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

IO異常的位置是否很重要?

以下是可致使故障的重要的日誌貓日誌: 11月7日至26日: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(); 
+0

我們可能會需要對你的代碼做什麼的詳細信息(步進通過它和解釋每個行並可能導致你的解決方案,太)和任何研究你已經爲這個問題做過了。例如,如果您遇到異常,什麼是異常?基本上,如果你認爲可以幫助某人回答這個問題,那麼添加它會很棒。 – nil 2012-07-25 10:48:19

+0

我所看到的是參數設置是有效的,並且能夠從logcat顯示,但是當通過Firefox的EXIF查看器打開內容時,我看不到任何寫在這張圖片中的GPS數據。 – 2012-07-25 10:51:47

回答

12

好吧,我這個掙扎了很長時間,但終於得到了它。我最後一次使用此代碼的工作:

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

      } 
+0

謝謝。我會試試 – 2012-07-26 02:14:57

+0

IOexception應該放在哪裏? – 2012-07-26 03:07:01

+0

您完成後是否重新保存了文件?此代碼適用於我,EXIF查看器可以看到嵌入位置 - 我只是對其進行了測試。 – Kaediil 2012-07-26 03:45:52