Search code examples
androidimageandroid-cameraexif

Android image resizing and preserving EXIF data (orientation, rotation, etc.)


If your Android app uses the device camera to take a picture and then resizes it (this is very, very common to reduce the size for upload), you might not realize that this resize operation strips the Exif metadata.

This can cause problems, especially if the device in question relies on the 'Orientation' tag to properly show the image upright.

Different Android devices handle camera/image rotation in different ways - my trusty old Nexus One seems to always rotate the image immediately post capture, so the file's native contents are always 'upright' when viewed.

However, other devices (especially Samsung phones in my testing), do not rotate the contents of the image file - rather, they set the Exif 'Orientation' tag. Whenever the image is displayed later, the relevant image code should detect the presence of the Orientation 'tag' and rotate the image appropriately. But if you have done any bitmap processing on the image and saved it to a new file, all of that Exif data is lost.

In addition to Orientation data, you might also lose other valuable metadata such as make/model, etc.

This confused me for a few weeks (image appears upright when displayed in phone gallery, but then arrives on my server with bad orientation and no apparent metadata). I'm adding this self-question here to help others. This blog post was very helpful:

Android re-size image without loosing EXIF information


Solution

  • As others have indicated, you must copy the Exif data from the original image to the final resized image. The Sanselan Android library is typically best for this. Depending on Android OS version, the ExifInterface sometimes corrupts the Exifdata.

    In addition, the ExifInterface also handles a limited number of Exif tags -- namely only the tags that it "knows" about. Sanselan on the other hand will keep all Exiftags and marker notes.

    Here is a blog post that shows how to use Sanselan for copying image data:

    Copying Exif metadata using Sanselan

    BTW, on Android I also tend to rotate the images and remove the Orientation Exiftag. For example, on a Nexus S with Android 4.03, the camera was setting an orientation tag in the Exifmetadata, but the webview was ignoring that information and displaying the image incorrectly. Sadly, rotating the actual image data and removing the Exiforientation tag is the only way to get every program to display images correctly.