How to save GPS coordinates in exif data on Android?

前端 未结 5 1427
借酒劲吻你
借酒劲吻你 2020-12-02 20:43

I am writing GPS coordinates to my JPEG image, and the coordinates are correct (as demonstrated by my logcat output) but it appears that it\'s being corrupted somehow. Readi

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 21:27

    Here is some code I've done to geotag my pictures. It's not heavily tested yet, but it seems to be ok (JOSM editor and exiftool read location).

    ExifInterface exif = new ExifInterface(filePath.getAbsolutePath());
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, GPS.convert(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, GPS.latitudeRef(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, GPS.convert(longitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, GPS.longitudeRef(longitude));
    exif.saveAttributes();
    

    And class GPS is here. (method could be shorter, but it's readable at least)

    /*
     * @author fabien
     */
    public class GPS {
        private static StringBuilder sb = new StringBuilder(20);
    
        /**
         * returns ref for latitude which is S or N.
         * @param latitude
         * @return S or N
         */
        public static String latitudeRef(double latitude) {
            return latitude<0.0d?"S":"N";
        }
    
        /**
         * returns ref for latitude which is S or N.
         * @param latitude
         * @return S or N
         */
        public static String longitudeRef(double longitude) {
            return longitude<0.0d?"W":"E";
        }
    
        /**
         * convert latitude into DMS (degree minute second) format. For instance
    * -79.948862 becomes
    * 79/1,56/1,55903/1000
    * It works for latitude and longitude
    * @param latitude could be longitude. * @return */ synchronized public static final String convert(double latitude) { latitude=Math.abs(latitude); int degree = (int) latitude; latitude *= 60; latitude -= (degree * 60.0d); int minute = (int) latitude; latitude *= 60; latitude -= (minute * 60.0d); int second = (int) (latitude*1000.0d); sb.setLength(0); sb.append(degree); sb.append("/1,"); sb.append(minute); sb.append("/1,"); sb.append(second); sb.append("/1000"); return sb.toString(); } }

提交回复
热议问题