From my application I can open gallery. Is there any way to get latitude and longitude of any selected image in gallery to my application?
Asked
Active
Viewed 1.5k times
4 Answers
13
You Should Go with ExifInterface class to read various EXIF metadata from Images:
Example :
ExifInterface exif = new ExifInterface(filepath);
exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
Edited :
Now Here you will get lat-long as Below format.
lat = 30/1,12/1,34/1, long=81/1,22/1,41/1
To Convert this into Real Values this Blog Helped Me.
we need to do conversion from degree, minute, second form to GeoPoint form.
By Below Way you can Do it.
String LATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String LATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String LONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String LONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
// your Final lat Long Values
Float Latitude, Longitude;
if((LATITUDE !=null)
&& (LATITUDE_REF !=null)
&& (LONGITUDE != null)
&& (LONGITUDE_REF !=null))
{
if(LATITUDE_REF.equals("N")){
Latitude = convertToDegree(LATITUDE);
}
else{
Latitude = 0 - convertToDegree(LATITUDE);
}
if(LONGITUDE_REF.equals("E")){
Longitude = convertToDegree(LONGITUDE);
}
else{
Longitude = 0 - convertToDegree(LONGITUDE);
}
}
private Float convertToDegree(String stringDMS){
Float 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 Float(FloatD + (FloatM/60) + (FloatS/3600));
return result;
};
@Override
public String toString() {
// TODO Auto-generated method stub
return (String.valueOf(Latitude)
+ ", "
+ String.valueOf(Longitude));
}
public int getLatitudeE6(){
return (int)(Latitude*1000000);
}
public int getLongitudeE6(){
return (int)(Longitude*1000000);
}

Bhavesh Patadiya
- 25,740
- 15
- 81
- 107
-
can you please tell me what is getExitTag...? – SREEJITH Mar 14 '13 at 09:30
-
1@SREEJITH :Oh.thanks for pointing it out. Actually i have Used the Method getExitTag(..) here. but Edited My Answer. you just have to use exif.getAttribute(TYPE); Method. – Bhavesh Patadiya Mar 14 '13 at 09:49
-
I am getting a value 10/1,16/1,3/1 instead of latitude. – SREEJITH Mar 14 '13 at 11:26
-
Pls tell me why it is like this – SREEJITH Mar 16 '13 at 11:48
-
@SREEJITH: edited my answer you can check it. and let me know if it will works for you. – Bhavesh Patadiya Mar 17 '13 at 03:23
-
Thanks for your code .And also there is small error in the code which I have edited. Thanks a lot for your support – SREEJITH Mar 18 '13 at 06:39
-
@BhaveshPatadiya I am getting error `java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.package/com.package.publicconnect.LevelFirst}: java.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference` I have written this in a separate class and call in `onActivityResult()` i am taking picture so i want to get the lat and long of that image. i am passing filename in the `exif..` and passing it to constructor. – Devendra Singh Nov 18 '15 at 13:51
-
http://stackoverflow.com/questions/33798250/how-to-get-location-and-other-information-from-image-in-android – Devendra Singh Nov 19 '15 at 08:08
-
Not working solution! exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) returns 0.0 values for latitude and longitude always. But coordinates are exists in image - in image viewer I can see this info. Anybody can help with issue? – Vitaly Apr 26 '22 at 04:26
-
@Vitaly path is incorrect format, did you get image from gallery? – Kanan Jul 15 '23 at 07:46
13
You can actually use a "buildin" function:
ExifInterface exif = new ExifInterface(path);
float[] latLong = new float[2];
boolean hasLatLong = exif.getLatLong(latLong)
if (hasLatLong) {
System.out.println("Latitude: " + latLong[0]);
System.out.println("Longitude: " + latLong[1]);
}
Maybe is something new, but I think is much more convenient than the accepted answer.

pauminku
- 3,526
- 3
- 22
- 24
0
the exif.getLatLong(float[])
is now deprecated, you can use a better method which returns a double[]
:
ExifInterface exifInterface = new ExifInterface(file);
double[] latlng = exifInterface.getLatLong();
if (latlng != null) {
Double currentLatitude = latlng[0];
Double currentLongitude = latlng[1];
Log.d("Debug", "Exif : latitude: " + currentLatitude + ", longitude: " + currentLongitude)
}
Happy coding.

Tom3652
- 2,540
- 3
- 19
- 45
0
This worked
val inputStream = context.contentResolver.openInputStream(contentUri)
val exif = inputStream?.let { it1 -> ExifInterface(it1) }
if (exif != null) {
val latLong = FloatArray(2)
if (exif.getLatLong(latLong)) {
Log.d("Debug", "lat ${latLong[0]} long ${latLong[1]}")
}
}

svg
- 114
- 1
- 8