2013-05-03 93 views
1

我正在嘗試編寫地理標記應用程序,用戶從照片庫中選擇圖像,然後該圖像通過其EXIF文件獲取當前GPS座標。如何將GPS座標寫入Android上的EXIF數據

到目前爲止,我可以拉起畫廊,選擇圖像,並找到我當前的GPS座標,但我無法將這些座標寫入EXIF文件。每當我選擇一個圖像,我都可以查看它,但沒有數據寫入EXIF文件。

我已經查找了幾個如何寫入EXIF的例子,並且我的代碼看起來正確(至少對我來說)。任何人都可以幫我弄清楚爲什麼我不寫任何數據?

這裏是我的代碼:

//place GPS cords into exif file 
     try { 
      ExifInterface exif = new ExifInterface("/sdcard/dcim/100MEDIA/IMAG0020.jpg"); 

      exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, DMSconv(lat)); 
      exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,DMSconv(lon)); 
      if (lat > 0) 
       exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N"); 
      else    
       exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S"); 
      if (lon > 0) 
       exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");  
      else    
       exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W"); 
      exif.saveAttributes(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

//convert from decimal degrees to DMS 
String DMSconv(double coord) { 
     coord = (coord > 0) ? coord : (-1)*coord; // -105.9876543 -> 105.9876543 
     String sOut = Integer.toString((int)coord) + "/1,"; // 105/1, 
     coord = (coord % 1) * 60;   // .987654321 * 60 = 59.259258 
     sOut = sOut + Integer.toString((int)coord) + "/1,"; // 105/1,59/1, 
     coord = (coord % 1) * 6000;    // .259258 * 6000 = 1555 
     sOut = sOut + Integer.toString((int)coord) + "/1000"; // 105/1,59/1,15555/1000 
     return sOut; 
    } 

感謝您的幫助!

編輯:此時我試圖將文件路徑和名稱硬編碼到ExifInterface中,所以這就是爲什麼它說"/sdcard/dcim/100MEDIA/IMAG0020.jpg"而不是filename。這可能與我的問題有關嗎?

+0

用於解決它。對文件路徑進行硬編碼是個問題。我使用了代碼 'int columnIndex = cursor.getColumnIndex(filePathColumn [0]); String picturePath = cursor.getString(columnIndex);' 並使用'picturePath'。 – user2340642 2013-05-04 05:16:16

回答

0

解決了它。對文件路徑進行硬編碼是個問題。我使用的代碼

 int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 

獲得picturePath,然後將其在

ExifInterface exif = new ExifInterface(picturePath);