Unable to change image's EXIF info?

Viewed 26

I want to change an image's EXIF info (its orientation to ExifInterface.ORIENTATION_ROTATE_270). I am using this code:

val inputStream: InputStream? = mActivity.contentResolver.openInputStream(imageURI)
val exif = ExifInterface(inputStream!!)

exif.setAttribute(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_ROTATE_270.toString())
exif.saveAttributes()

Unfortunately, this code has no effect. Why?

1 Answers

I would expect that code to be throwing an IOException based on the ExifInterface source code. All you gave ExifInterface is an InputStream; ExifInterface has no way to write to one of those. You will need to:

  • Use the InputStream to copy the image to a file that you control (e.g., in getCacheDir() on Context)
  • Use ExifInterface with that file to make your desired changes
  • Use openOutputStream() on ContentResolver to get an OutputStream back to the original content
  • Copy the contents of your modified file to that OutputStream
Related