Read the Exif tag from the Image and also Read the location

 

Read the Exif tag from the Image and also Read the location

The ExifInterface class in Android development is widely utilized to store metadata, including camera settings, orientation, and even geographical coordinates, within image files using the Exchangeable image file format (Exif). If you require programmatic extraction of this information in your Android application, the ExifInterface class offers a convenient approach to accessing Exif metadata.

Certainly! Here is an illustration showcasing the utilization of the metadata-extractor library in an Android application to extract metadata, including Exif data, from an image file.

Step 1: Dependency Inclusion

In the build. gradle file of your app, you add the metadata-extractor library as a dependency.

dependencies {
// Other dependencies

implementation("com.drewnoakes:metadata-extractor:2.18.0")
}

Step 2: Permissions

The AndroidManifest.xml file of the application contains the required permission to access external storage.

//This permission start is used for api level 33 
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

Step 3: Reading Metadata and Also Giving Runtime Image Read Permission.

Upon executing the application, it will record pertinent details regarding the metadata present in the designated image file. This comprehensive information encompasses camera configurations, date, time, and other pertinent metadata.

class MainActivity : AppCompatActivity() {

// Declare UI elements
lateinit var button: AppCompatButton
lateinit var imageView: AppCompatImageView
lateinit var text: AppCompatTextView

// Declare a data class to store image details
lateinit var imageDeatil: ImageDeatil

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize UI elements
button = findViewById(R.id.btn_select)
imageView = findViewById(R.id.img_select)
text = findViewById(R.id.text_data)

// Set click listener for the button
button.setOnClickListener {
// Request permission to read external storage based on Android version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermessionLauncher.launch(READ_MEDIA_IMAGES)
} else {
requestPermessionLauncher.launch(READ_EXTERNAL_STORAGE)
}
}
}

// Register permission launcher for handling permission result
val requestPermessionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
// Launch the photo picker if permission is granted
launchNewPhotoPicker()
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show()
}
}

// Function to launch the new photo picker
private fun launchNewPhotoPicker() {
newPhotoPiker.launch("image/*")
}

// Register activity result launcher for getting content (selected image)
val newPhotoPiker = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
// Set the selected image in the ImageView
imageView.setImageURI(uri)

// Use metadata-extractor to read metadata from the selected image
lifecycleScope.launch(Dispatchers.Main) {
val result = async(Dispatchers.IO) {
val inputStream = uri?.let { contentResolver.openInputStream(it) }
val metadata = ImageMetadataReader.readMetadata(inputStream)

// Iterate through metadata directories and tags
for (directory in metadata.directories) {
for (tag in directory.tags) {
Log.i("TAG", "Metadata Tag: $tag")
}
}

// Extract specific information (example: image height, model, latitude, longitude)
val jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory::class.java)
val imageHeight = jpegDirectory?.getString(JpegDirectory.TAG_IMAGE_HEIGHT)

val exifDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory::class.java)
val model = exifDirectory?.getString(ExifIFD0Directory.MO)
val imagname = exifDirectory?.getString(ExifIFD0Directory.TAG_DATETIME)

val gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory::class.java)
val latitude = gpsDirectory?.geoLocation?.latitude
val longitude = gpsDirectory?.geoLocation?.longitude

Triple(imageHeight, model, latitude, longitude)
}

// Retrieve values from the result
val (imageHeight, model, latitude, longitude) = result.await()






// Update the UI with extracted metadata
text.text =
"Image Height: $imageHeight\nModel Name: $model\nLatitude: $latitude\nLongitude: $longitude"
}
}
}

The provided Kotlin code showcases an Android application that utilizes a metadata-extractor to retrieve image metadata. Notable aspects of this code include the ability to handle permissions dynamically, integrate a photo picker, and extract various metadata such as image height, model, and location, The application effectively presents the metadata in the user interface and logs the stored information. This succinct example serves as a practical and adaptable solution for extracting image metadata, catering to specific requirements.

Project Link: GitHub

Thank you. Please follow and comment

Comments

Popular posts from this blog

How to Integrate or Work with Open Street Map (OSM) in an Android App (Kotlin)

ListView in android with api and volley Network Library in android Studio.

how to implement OpenStreetMap in android