I have a method in my Android application that extracts the location (longitude and latitude) from a photo using ExifInterface. The code works fine on Android 10 but fails on Android 13. Here’s the method:
private fun getLocationAndMoveOn() {
if (hasMediaLocationPermission(requireContext())) {
val pickedPhotoData = libraryViewModel.viewState.pickedPhoto.value?.DATA ?: return
if (hasSdkHigherThan(Build.VERSION_CODES.P)) {
val uri = Uri.parse("file://$pickedPhotoData")
activity?.contentResolver?.openInputStream(uri)?.use { stream ->
ExifInterface(stream).run {
val latLong = FloatArray(2)
if (getLatLong(latLong)) {
val location = Location("").apply {
latitude = latLong[0].toDouble()
longitude = latLong[1].toDouble()
}
navigateToLocation(location)
} else {
showPhotoLocationDataDialog()
}
}
}
} else {
val location = GeoDataUtils.geoDegree(ExifInterface(pickedPhotoData))
navigateToLocation(location)
}
} else {
requestMediaLocationPermission(requireActivity(), REQUEST_PERMISSION_MEDIA_ACCESS)
}
}
What the code does:
• It takes a file address (pickedPhotoData) and, using ExifInterface, gets the location where the photo was taken.
Issue:
• This code works fine on Android 10.
• On Android 13, getLatLong(latLong) always returns false, indicating that the location data cannot be retrieved from the photo’s EXIF metadata.
What I’ve tried:
• Checked permissions to ensure the app has the necessary permissions to access the photo.
• Verified that the photo contains location metadata.
• Looked into alternative libraries but haven’t found a suitable solution yet.
My question:
• How can I reliably get the location (longitude and latitude) from a photo on Android 13?
• Are there any modern libraries or methods to handle this that work across different Android versions?
Additional information:
• I’ve ensured the app has the necessary permissions to read media files.
• The photo does contain location metadata when checked on other devices and software.
Any help or pointers would be greatly appreciated!