Android write EXIF GPS Latitude and Longitude onto JPEG failed

11,573

Ok, I struggled with this for a long time but finally got it. Last time I used it this code worked:

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

          }
Share:
11,573

Related videos on Youtube

Larry Lo
Author by

Larry Lo

Code with passion.

Updated on September 15, 2022

Comments

  • Larry Lo
    Larry Lo over 1 year

    I would like to add GPS data such as longitude and latitude into the jpeg photo. The photo is captured by tabbing the card (NFC) In the logcat correct values can be displayed but these values cannot be written into the jpg photo file !

    Below is my code: It is used to take saved jpg files and call the method below The method is used to add EXIF GPS parameters into the jpg The GPS parameters such as longitude and latitude are already taken in another activity.

    I use EXIF Viewer in Firefox to see the result.

    Does the position for the IO Exception matters?

    The following is the important log cat log which may render the failure: 07-26 11:48:30.386: D/NativeNfcTag(195): Tag lost, restarting polling loop

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

    The below is the position to call the method ...

        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();
    
  • Larry Lo
    Larry Lo almost 12 years
    Where the IOexception should be placed ?
  • Kaediil
    Kaediil almost 12 years
    Did you resave the file when you are done? This code works for me and EXIF viewer sees the embedded position - I just tested it.
  • Larry Lo
    Larry Lo almost 12 years
    what else should I save besides adding exif.saveAttributes(); ? And which software is the best to check the EXIF ?
  • Kaediil
    Kaediil almost 12 years
    I used the EXIF viewer in Firefox. I sent the file to myself from my phone, opened it with that viewer then scrolled down and it showed the GPS. It even provided links to show the location on google maps and it was spot on. Are you sure you have a valid GPS location?
  • Larry Lo
    Larry Lo almost 12 years
    When I use maps.google.com and enter the co-ordinate as 22.3071491,114.1719491 it is valid
  • Kaediil
    Kaediil almost 12 years
    Sorry, dunno what the problem is then, I know it should work and is for me.
  • Larry Lo
    Larry Lo almost 12 years
    when I use FxiF , the insertion of GPS data is successful
  • Yusan Susandi
    Yusan Susandi over 11 years
    Hi Kaediil.. i wanna ask about this code, how to running in javascript? I need for take picture with geolocation in my android device. Thanks in advance.
  • Larry Lo
    Larry Lo about 11 years
    Excuse me ,are you talking about how to display the Geo information in the webpage using javascript ?
  • Benjamin Toueg
    Benjamin Toueg over 10 years
    I needed to implement the following fix: splits[0] + "/1," + splits[1] + "/1," + seconds.replace(',','.') + "/1";