Android Question Access Exif data in SMM

gunnarjonsson

Member
Licensed User
Longtime User
Hi All,
I saw en example how image orientation was accessed in SMM.

#if B4A
If B4ASdkVersion >= 24 Then
Dim ExifInterface As JavaObject
ExifInterface.InitializeNewInstance("android.media.ExifInterface", Array(In))
Dim orientation As Int = ExifInterface.RunMethod("getAttribute", Array("Orientation"))
Media.Media = RotateBitmapBasedOnOrientation(Media.Media, orientation)
End If
In.Close
#Else If B4J AND SMM_META
Dim MetadataReader As JavaObject
Dim ExifReader As JavaObject
ExifReader.InitializeNewInstance("com.drew.metadata.exif.ExifReader", Null)
Dim readers As List = Array(ExifReader)
Try
Dim Metadata As JavaObject = MetadataReader.InitializeStatic("com.drew.imaging.jpeg.JpegMetadataReader").RunMethod("readMetadata", Array(In, readers))
For Each dic As JavaObject In Metadata.RunMethod("getDirectories", Null).As(List)
Dim orientation As Object = dic.RunMethod("getInteger", Array(274)) 'orientation
If orientation <> Null Then
Media.Media = RotateBitmapBasedOnOrientation(Media.Media, orientation)
Exit
End If
Next
Catch
Log(LastException)
End Try
In.Close
#End If

How would I go about obtaining the LatLon values in a separate routine within my own code.
I have tried jpegutils but I can not get it to work. SMM seems to offer a simpler method.
I am most interested in B4A

Gunnar
 

MarcoRome

Expert
Licensed User
Longtime User
Hi @gunnarjonsson
Pls insert your thread correctly. Enter the code in "code"

1680711025981.png


1680711035261.png


Thank you
 
Upvote 0

gunnarjonsson

Member
Licensed User
Longtime User
B4X:
#if B4A
If B4ASdkVersion >= 24 Then
Dim ExifInterface As JavaObject
ExifInterface.InitializeNewInstance("android.media.ExifInterface", Array(In))
Dim orientation As Int = ExifInterface.RunMethod("getAttribute", Array("Orientation"))
Media.Media = RotateBitmapBasedOnOrientation(Media.Media, orientation)
End If
In.Close
#Else If B4J AND SMM_META
Dim MetadataReader As JavaObject
Dim ExifReader As JavaObject
ExifReader.InitializeNewInstance("com.drew.metadata.exif.ExifReader", Null)
Dim readers As List = Array(ExifReader)
Try
Dim Metadata As JavaObject = MetadataReader.InitializeStatic("com.drew.imaging.jpeg.JpegMetadataReader").RunMethod("readMetadata", Array(In, readers))
For Each dic As JavaObject In Metadata.RunMethod("getDirectories", Null).As(List)
Dim orientation As Object = dic.RunMethod("getInteger", Array(274)) 'orientation
If orientation <> Null Then
Media.Media = RotateBitmapBasedOnOrientation(Media.Media, orientation)
Exit
End If
Next
Catch
Log(LastException)
End Try
In.Close
#End If
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Dim orientation As Int = ExifInterface.RunMethod("getAttribute", Array("Orientation"))
Replace the above with
B4X:
Dim LatLongInfo(2) As Float
Dim hasLatLong As Boolean = ExifInterface.RunMethod("getLatLong", Array(LatLongInfo))
If hasLatLong Then
   Log($"Latitude: ${LatLongInfo(0)}"$)
   Log($"Longitude: ${LatLongInfo(1)}"$)
Else
   Log("No location information contained in the EXIF information")
End If
Note: Not tested and may contain typos/logical errors
Source:
 
Upvote 0

gunnarjonsson

Member
Licensed User
Longtime User
Hi!
Now I have tried it.
The good news is that the code seems to work but the bad news is that I still get the latitude and longitud values as zero. This is for photos that I have verified have values via 3rd party tools. Can there be a recent demand for some new permission to get these values?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Can there be a recent demand for some new permission to get these values?
Maybe:
For anyone who face this issue, i'v found the reason.

In Android with SDK >=29, you need ACCESS_MEDIA_LOCATION permission.

<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
Request for permission before pick image and then GPS will return correctly ?
Link: https://github.com/ionic-team/capacitor-plugins/issues/1074#issuecomment-1172220162
 
Upvote 0
Top