3

I am trying to extract location coordinates from image media in my Android app but ExifInterface providing a null value even though image media contains those details. I had tried on Android 9, 10, and 11 OS emulators and devices.

I am using android-image-picker library to provide image selection in my app. I believe that is not related to this image library.

I am also requesting ACCESS_MEDIA_LOCATION for Android 10 and later devices as documented here. I am still wondering if this is the case then it should work on an Android 9 device but no luck so far with that as well.

Please find image files here in dropbox to make sure they persist metadata details.

In debugging it shows attributes loaded in the ExifInterface object but when the app is trying to read it then it just returns null. Please take a look at debugging screenshot of the image (name: IMG_20210916_170621.jpg) from the above-shared images. The same image does show location details in the macOS Preview app as well as available Exif metadata reader websites (See screenshot below).

  • GPS details screenshot:

GPS details

  • Debugging screenshot:

Debugging screenshot

  • Code:
@Parcelize
data class Image(override val url: String) : LocalMedia(url) {
    override fun toString(): String {
        return url
    }

    fun readLatLong(): Pair<Double, Double>? {
        try {
            val exifInterface = ExifInterface(url)
            exifInterface.latLong?.let {
                if (it.size == 2) {
                    return Pair(it.first(), it.last())
                }
            }
        } catch (e: Exception) {
            Timber.e(e, "Reading latLong failed")
        }
        return null
    }
}

I hope I am not misinterpreting anything here.

Bipin Vayalu
  • 3,025
  • 2
  • 25
  • 39
  • The item you have expanded in your debugger is not the latitude, but rather [the "latitude ref"](https://developer.android.com/reference/androidx/exifinterface/media/ExifInterface#TAG_GPS_LATITUDE_REF) (whether a latitude is north or south). If you want the latitude, you need to request [the latitude](https://developer.android.com/reference/androidx/exifinterface/media/ExifInterface#TAG_GPS_LATITUDE). – CommonsWare Sep 17 '21 at 11:05
  • Based on [the source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:exifinterface/exifinterface/src/main/java/androidx/exifinterface/media/ExifInterface.java;l=5031-5057?q=exifinterface&ss=androidx), if you are getting `null` for `getLatLng()`, then the image is missing one or more of the location tags (latitude, latitude ref, longitude, longitude ref) or the values are not parseable for some reason. Since you are using the AndroidX `ExifInterface` (good choice!), you should be able to step into `getLatLng()` and see what is missing. – CommonsWare Sep 17 '21 at 11:06
  • `Please find image files here in dropbox to make sure they persist metadata details.` Do you mean latitude and longitude in exif header of jpg file? – blackapps Sep 17 '21 at 11:14
  • Examined your first picture and the exif header contains lat,lon. But where is your code? – blackapps Sep 17 '21 at 11:21
  • Thanks @CommonsWare. I just tried to debug `getLatLng()` method for the same image and I am receiving null for lat & lng and empty string for lat & lng references. But the same image shows location details in Android Photo app, macOS preview app and also on the online metadata reader websites. – Bipin Vayalu Sep 17 '21 at 12:58
  • @blackapps I just added my code of reading latlng using ExifInterface. – Bipin Vayalu Sep 17 '21 at 12:59
  • "I believe that is not related to this image library" -- you might still want to run some experiments, such as packaging your image as an asset and reading from there, or downloading the image from some Web site to `getCacheDir()` and reading it from your downloaded copy. – CommonsWare Sep 17 '21 at 13:03
  • Please give examples of path or uri values this image picker delivers. – blackapps Sep 17 '21 at 13:11
  • Once you know filename IMG_20210916_170621.jpg you do not need an image picker but could directly load from /storage/emulated/0/DCIM/Camera/IMG_20210916_170621.jpg or from /storage/emulated/0/Pictures/IMG_20210916_170621.jpg depending on Android version of used device. Dont test with an Android 11 device as you need yet another permission then. – blackapps Sep 17 '21 at 13:14
  • @CommonsWare I did testing by placing the same image in cache, assets, and picking up from gallery Still, I am not getting latlng value but I found that when I am loading image form assets / cache, it shows me lat & lng ref value to N/W string. I am using Android 10 emulator for testing. – Bipin Vayalu Sep 17 '21 at 13:53
  • yes @blackapps I had tested on the Android 9, 10 and 11 (Added required permission request) all three variant and same result. I am passing file path to ExifInterface directly and path is my case is: `/storage/emulated/0/Pictures/IMG_20210916_170621.jpg`. – Bipin Vayalu Sep 17 '21 at 13:55
  • `and 11 (Added required permission request) ` Not the one you mentioned here. – blackapps Sep 17 '21 at 13:59
  • @blackapps Ok, So which other permission we needed for Android 11? Btw, It's not working in Android 9 and 10 devices as too. – Bipin Vayalu Sep 21 '21 at 10:08
  • Bring it to work on 9 and 10 first. For 11+ i think you need 'access all files permission'. – blackapps Sep 21 '21 at 10:21
  • Yes, @blackapps That is the issue that it's not working in Android 9 and 10. I believe I followed the proper guidelines of extracting location metadata from images but it's not working for 9 and 10. So once it is fixed then I can move ahead and figure out permission for Android 11. – Bipin Vayalu Sep 23 '21 at 06:13
  • in android 9 it works without any permission . are you using androidx.exifinterface.media.ExifInterface ? – Arvin Rezaei Oct 02 '21 at 13:34
  • Yes @ArvinRezaei I am using androidx.exifinterface.media.ExifInterface. – Bipin Vayalu Oct 05 '21 at 06:20

0 Answers0