If you’re working with images in Android and need to extract metadata like orientation, timestamp, or GPS location, EXIF data is the key. The EXIF (Exchangeable Image File Format) metadata is embedded inside JPEG files and contains useful information captured by the camera.
In this guide, we’ll show you how to read EXIF data from a stored image using both Java and Kotlin.
📌 What is EXIF Data?
EXIF metadata includes:
- Image orientation
- Date and time
- GPS coordinates
- Camera settings (ISO, shutter speed, etc.)
This data is especially useful when handling image rotation or location-based tagging.
✅ Reading EXIF Data Using Java
import android.media.ExifInterface;
import java.io.IOException;
public void readExifData(String imagePath) {
try {
ExifInterface exif = new ExifInterface(imagePath);
String dateTime = exif.getAttribute(ExifInterface.TAG_DATETIME);
String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
String make = exif.getAttribute(ExifInterface.TAG_MAKE);
String model = exif.getAttribute(ExifInterface.TAG_MODEL);
float[] latLong = new float[2];
if (exif.getLatLong(latLong)) {
float latitude = latLong[0];
float longitude = latLong[1];
// Use latitude and longitude
}
// Use other data as needed
} catch (IOException e) {
e.printStackTrace();
}
}
Make sure the imagePath
is the absolute path to the image stored on the device, e.g., from internal storage or gallery.
✅ Reading EXIF Data Using Kotlin
import android.media.ExifInterface
import java.io.IOException
fun readExifData(imagePath: String) {
try {
val exif = ExifInterface(imagePath)
val dateTime = exif.getAttribute(ExifInterface.TAG_DATETIME)
val orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION)
val make = exif.getAttribute(ExifInterface.TAG_MAKE)
val model = exif.getAttribute(ExifInterface.TAG_MODEL)
val latLong = FloatArray(2)
if (exif.getLatLong(latLong)) {
val latitude = latLong[0]
val longitude = latLong[1]
// Use the coordinates
}
// Use or display the metadata as needed
} catch (e: IOException) {
e.printStackTrace()
}
}
📂 Where to Get Image Path
If the image comes from a Uri
, you may need to convert it to a file path or open an input stream and use a different ExifInterface
constructor:
val inputStream = context.contentResolver.openInputStream(uri)
val exif = inputStream?.let { ExifInterface(it) }
📌 Note: The stream-based constructor is required for content URIs starting from Android Q (API 29).
🧭 Common EXIF Tags to Know
Tag | Description |
---|---|
TAG_DATETIME | Date & time the photo was taken |
TAG_ORIENTATION | Image orientation (used to rotate properly) |
TAG_MAKE | Camera manufacturer |
TAG_MODEL | Camera model |
TAG_GPS_LATITUDE / TAG_GPS_LONGITUDE | Location data |
🛠️ Pro Tips
- Always check for
null
values in attributes. - Consider using EXIF orientation to automatically rotate images in your app.
- EXIF data is only reliable if the camera app saves it—some image sources (like screenshots) may not include EXIF.
Final Thoughts
Reading EXIF data from images is essential for apps that handle camera input, photo galleries, or GPS tagging. With the ExifInterface
class in Android, accessing metadata is straightforward whether you’re using Java or Kotlin.
Use this metadata to improve image handling, rotation, and user experience in your Android apps.
Helpful Links: