Android Copy EXIF Data (Kotlin)

June 18, 2018
fun copyExif(oldPath: String, newPath: String) {
    val oldExif = ExifInterface(oldPath)

    val attributes = arrayOf(
            // ExifInterface.TAG_APERTURE,
            ExifInterface.TAG_APERTURE_VALUE,
            ExifInterface.TAG_DATETIME,
            ExifInterface.TAG_DATETIME_DIGITIZED,
            ExifInterface.TAG_DATETIME_ORIGINAL,
            ExifInterface.TAG_EXPOSURE_TIME,
            ExifInterface.TAG_FLASH,
            ExifInterface.TAG_FOCAL_LENGTH,
            ExifInterface.TAG_GPS_ALTITUDE,
            ExifInterface.TAG_GPS_ALTITUDE_REF,
            ExifInterface.TAG_GPS_DATESTAMP,
            ExifInterface.TAG_GPS_LATITUDE,
            ExifInterface.TAG_GPS_LATITUDE_REF,
            ExifInterface.TAG_GPS_LONGITUDE,
            ExifInterface.TAG_GPS_LONGITUDE_REF,
            ExifInterface.TAG_GPS_PROCESSING_METHOD,
            ExifInterface.TAG_GPS_TIMESTAMP,
            // ExifInterface.TAG_IMAGE_LENGTH,
            // ExifInterface.TAG_IMAGE_WIDTH,
            ExifInterface.TAG_ISO,
            ExifInterface.TAG_ISO_SPEED_RATINGS,
            ExifInterface.TAG_MAKE,
            ExifInterface.TAG_MODEL,
            ExifInterface.TAG_ORIENTATION,
            ExifInterface.TAG_SUBSEC_TIME,
            ExifInterface.TAG_SUBSEC_TIME_ORIGINAL,
            ExifInterface.TAG_SUBSEC_TIME_DIGITIZED,
            // ExifInterface.TAG_SUBSEC_TIME_DIG,
            // ExifInterface.TAG_SUBSEC_TIME_ORIG,
            ExifInterface.TAG_WHITE_BALANCE)

    val newExif = ExifInterface(newPath)
    for (i in attributes.indices) {
        val value = oldExif.getAttribute(attributes[i])
        if (value != null)
            newExif.setAttribute(attributes[i], value)
    }
    newExif.saveAttributes()
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.