'Save and Retrieve location inside JPEG image

Am going to share a solution which include save and retrieve location inside a JPEG image file.The latitude and longitude is save and retrive inside the image metadata using ExifInterface More about ExifInterface can be found here http://developer.android.com/reference/android/media/ExifInterface.html



Solution 1:[1]

Using ExifInterface you can get following information from media.

Variable Declaration.

String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;;
Double Latitude, Longitude;
List<Address> addresses;
Geocoder geocoder;

User ExifInterface using following way.

ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>);
// This will give you data and time
mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME);

attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
  valid = true;

  if (attrLATITUDE_REF.equals("N")) {
    Latitude = convertToDegree(attrLATITUDE);
  } else {
    Latitude = 0 - convertToDegree(attrLATITUDE);
  }
  if (attrLONGITUDE_REF.equals("E")) {
    Longitude = convertToDegree(attrLONGITUDE);
  } else {
    Longitude = 0 - convertToDegree(attrLONGITUDE);
  }

  try {
    addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
  } catch (IOException e) {
    e.printStackTrace();
  }
  if (addresses != null && addresses.size() > 0) {
    zip = addresses.get(0).getPostalCode();
    city = addresses.get(0).getLocality();
    state = addresses.get(0).getAdminArea();
    country = addresses.get(0).getCountryName();
    if (zip != null) {
      title += zip + ",";
    }
    if (city != null) {
      title += city + ",";
    }
    if (state != null) {
      title += state + ",";
    }
    if (country != null) {
      title += country;
    }
  } else {
    title = "Unknown Location";
  }
}

private Double convertToDegree(String stringDMS) {
  Double result = null;
  String[] DMS = stringDMS.split(",", 3);

  String[] stringD = DMS[0].split("/", 2);
  Double D0 = new Double(stringD[0]);
  Double D1 = new Double(stringD[1]);
  Double FloatD = D0 / D1;

  String[] stringM = DMS[1].split("/", 2);
  Double M0 = new Double(stringM[0]);
  Double M1 = new Double(stringM[1]);
  Double FloatM = M0 / M1;

  String[] stringS = DMS[2].split("/", 2);
  Double S0 = new Double(stringS[0]);
  Double S1 = new Double(stringS[1]);
  Double FloatS = S0 / S1;

  result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));

  return result;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xxx