Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android write EXIF GPS Latitude and Longitude onto JPEG failed

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();
like image 633
Raju yourPepe Avatar asked Jul 25 '12 07:07

Raju yourPepe


1 Answers

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

          }
like image 59
Kaediil Avatar answered Sep 23 '22 14:09

Kaediil