2

I'm trying to add gps location tags to jpg images. These images are pictures that my app takes with the android back camera, then converts to a bitmap, edits and finally saves as JPEGs.

Once an image is saved, I try to add the gps data using the ExifInterface. My code for formatting and saving the gps tag is based on the answer on this question.

public void geoTag(String filePath, double latitude, double longitude){
    ExifInterface exif;

    try {
        File file = new File(filePath);
        exif = new ExifInterface(file);
        int num1Lat = (int)Math.floor(latitude);
        int num2Lat = (int)Math.floor((latitude - num1Lat) * 60);
        double num3Lat = (latitude - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

        int num1Lon = (int)Math.floor(longitude);
        int num2Lon = (int)Math.floor((longitude - num1Lon) * 60);
        double num3Lon = (longitude - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");


        if (latitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
        }

        if (longitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
        }
        exif.saveAttributes();
    } catch (IOException e) {
        Log.e("PictureActivity", e.getLocalizedMessage());
    }

}

When I call this method, no tags are added to my jpg image. My latitude and longitude arguments are correct, I get them from the system's location service, for which I have the required permissions. The filepath is correct, the file exists. Creating the ExifInterface does not throw any exceptions. Calling saveAttributes() doesn't throw any exceptions either.

I tried adding another tag to see if maybe my formatting was incorrect.

exif.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP, "10:00:00");

But that didn't show up in my jpg's info either.

My minSdkVersion is set to 23 and my targetSdkVersion is 29. At first I used android.media.ExifInterface, then I switched to androidx.exifinterface.media.ExifInterface, but that didn't help either.

This is what I'm getting, but it's this that I want.

MDoor
  • 21
  • 5

1 Answers1

0

Turns out that the code does work and the gps data is added in the exif meta data, but android does not seem to recognize the format. The data is visible when looked at via the image properties on windows or viewed in an online exif viewer.

When I use setGpsInfo(Location location) some android versions do seem to recognise the format and display it.

Phydiux's answer to this question also confirmed my suspicions about formatting.

MDoor
  • 21
  • 5