Wednesday, December 21, 2016

Introducing the ExifInterface Support Library

With the release of the href="https://developer.android.com/topic/libraries/support-library/revisions.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#rev25-1-0">25.1.0 Support Library, there's a new entry in the family: the ExifInterface Support Library. With significant improvements introduced in Android 7.1 to the framework's href="https://developer.android.com/reference/android/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog">ExifInterface, it only made sense to make those available to all API 9+ devices via the Support Library's ExifInterface.

The basics are still the same: the ability to read and write href="https://en.wikipedia.org/wiki/Exif?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog">Exif tags embedded within image files: now with 140 different attributes (almost 100 of them new to Android 7.1/this Support Library!) including information about the camera itself, the camera settings, orientation, and GPS coordinates.

Camera Apps: Writing Exif Attributes

For Camera apps, the writing is probably the most important - writing attributes is still limited to JPEG image files. Now, normally you wouldn't need to use this during the actual camera capturing itself - you'd instead be calling the Camera2 API href="https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Builder.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#set(android.hardware.camera2.CaptureRequest.Key%3CT%3D,%20T)">CaptureRequest.Builder.set() with href="https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#JPEG_ORIENTATION">JPEG_ORIENTATION, href="https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#JPEG_GPS_LOCATION">JPEG_GPS_LOCATION or the equivalents in the Camera1 href="https://developer.android.com/reference/android/hardware/Camera.Parameters.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog">Camera.Parameters. However, using ExifInterface allows you to make changes to the file after the fact (say, removing the location information on the user's request).

Reading Exif Attributes

For the rest of us though, reading those attributes is going to be our bread-and-butter; this is where we see the biggest improvements.

Firstly, you can read Exif data from JPEG and raw images (specifically, DNG, CR2, NEF, NRW, ARW, RW2, ORF, PEF, SRW and RAF files). Under the hood, this was a major restructuring, removing all native dependencies and building an extensive test suite to ensure that everything actually works.

For apps that receive images from other apps with a content:// URI (such as those sent by apps that href="https://developer.android.com/about/versions/nougat/android-7.0-changes.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#sharing-files">target API 24 or higher), ExifInterface now works directly off of an InputStream; this allows you to easily extract Exif information directly out of content:// URIs you receive without having to create a temporary file. class="prettyprint">Uri uri; // the URI you've received from the other app
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
ExifInterface exifInterface = new ExifInterface(in);
// Now you can extract any Exif tag you want
// Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
// Handle any errors
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}

Note: ExifInterface will not work with remote InputStreams, such as those returned from a href="https://developer.android.com/reference/java/net/HttpURLConnection.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog">HttpURLConnection. It is strongly recommended to only use them with content:// or file:// URIs.

For most attributes, you'd simply use the href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getAttributeInt(java.lang.String,%20int)">getAttributeInt(), href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getAttributeDouble(java.lang.String,%20double)">getAttributeDouble(), or href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getAttribute(java.lang.String)">getAttribute() (for Strings) methods as appropriate.

One of the most important attributes when it comes to displaying images is the image orientation, stored in the aptly-named href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#TAG_ORIENTATION">TAG_ORIENTATION, which returns one of the ORIENTATION_ constants. To convert this to a rotation angle, you can post-process the value. class="prettyprint">int rotation = 0;
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
}

There are some helper methods to extract values from specific Exif tags. For location data, the href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getLatLong(float[])">getLatLong() method gives you the latitude and longitude as floats and href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getAltitude(double)">getAltitude() will give you the altitude in meters. Some images also embed a small thumbnail. You can check for its existence with href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#hasThumbnail()">hasThumbnail() and then extract the byte[] representation of the thumbnail with href="https://developer.android.com/reference/android/support/media/ExifInterface.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#getThumbnail()">getThumbnail() - perfect to pass to href="https://developer.android.com/reference/android/graphics/BitmapFactory.html?utm_campaign=android_launch_exifsupportlibrary_122116&utm_source=anddev&utm_medium=blog#decodeByteArray(byte[],%20int,%20int)">BitmapFactory.decodeByteArray().

Working with Exif: Everything is optional

One thing that is important to understand with Exif data is that there are no required tags: each and every tag is optional - some services even specifically strip Exif data. Therefore throughout your code, you should always handle cases where there is no Exif data, either due to no data for a specific attribute or an image format that doesn't support Exif data at all (say, the ubiquitous PNGs or WebP images).

Add the ExifInterface Support Library to your project with the following dependency:

class="prettyprint">compile "com.android.support:exifinterface:25.1.0"

But when an Exif attribute is exactly what you need to prevent a mis-rotated image in your app, the ExifInterface Support Library is just what you need to #BuildBetterApps