Store GPS Location in image background android.

Store GPS Location in image background android, store latitude and longitude value in the image.

Solution:


ExifInterface exif;

//get the value from google or network operator.
double latitude = getLatitude();
double longitude = getLongitude();

try {
//pass image file name after capturing an image or an image picked from the gallery.

    exif = new ExifInterface("/sdcard/DCIM/"+filename+".jpeg");

    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;

//saveAttributes to exif
    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());
}


Detail provided here:

https://developer.android.com/reference/android/media/ExifInterface


Comments

Popular Post

Implement search functionality with help of edittext, rxjava and retrofit 2 in android.

QR code scanner using Zxing library. Scanner for android

Storing captured image or an image picked from a gallery in internal storage using file provider android.

Shortcut for android for every version, android shortcut for oreo, android shortcut for nougat.