views:

635

answers:

3

Is there any 3rd part api for android to read exif tags from image which support api level starting from 1.5.

+1  A: 

The metadata extraction library by Drew Nokes works well for extracting EXIF tags on earlier Android platform versions, with a slight modification. I am using it on Android 1.6 to extract tags from JPEG images.

You will need to download and build the source code yourself, and package it with your app. (I'm using release 2.3.1.) Make the following changes to com.drew.imaging.jpeg.JpegMetadataReader:

  • Remove the following import statement:

    import com.sun.image.codec.jpeg.JPEGDecodeParam;

  • Delete the following method (which you won't need on Android):

    public static Metadata readMetadata(JPEGDecodeParam decodeParam) { ... }

Remove the com.drew.metadata.SampleUsage class, which references the method deleted above. Also remove all of the test packages.

That's all there is to it. Here's an example of using the JpegMetadataReader to extract a date-time tag from a JPEG image stored on the SD card:

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifDirectory;

// other imports and class definition removed for brevity

public static Date extractExifDateTime(String imagePath)
{
    Log.d("exif", "Attempting to extract EXIF date/time from image at " + imagePath);
    Date datetime = new Date(0); // or initialize to null, if you prefer
    try
    {
        Metadata metadata = JpegMetadataReader.readMetadata(new File(imagePath));
        Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);

        // these are listed in order of preference
        int[] datetimeTags = new int[] { ExifDirectory.TAG_DATETIME_ORIGINAL,
                                         ExifDirectory.TAG_DATETIME,
                                         ExifDirectory.TAG_DATETIME_DIGITIZED };
        int datetimeTag = -1;
        for (int tag : datetimeTags)
        {
            if (exifDirectory.containsTag(tag))
            {
                datetimeTag = tag;
                break;
            }
        }

        if (datetimeTag != -1)
        {
            Log.d("exif", "Using tag " + exifDirectory.getTagName(datetimeTag) + " for timestamp");

            SimpleDateFormat exifDatetimeFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            datetime = exifDatetimeFormat.parse(exifDirectory.getString(datetimeTag));
        }
        else
        {
            Log.d("exif", "No date/time tags were found");
        }
    }
    catch (Exception e)
    {
        Log.w("exif", "Unable to extract EXIF metadata from image at " + imagePath, e);
    }
    return datetime;
}
S. Farley
A: 

For what it worth, did you try to use the native ExifInterface class ?

http://developer.android.com/reference/android/media/ExifInterface.html

Should be must faster than using a 3rd party library ;)

Spredzy
The question specifically asks for how to do this from Android 1.5 and up. The ExifInterface was introduced in Android 2.0 (API level 5). For 2.0 and higher, then the ExifInterface class should work.
S. Farley
A: 

Hi! I am trying to develop an app targetting android 1.6. I let the user select an image and then uploads said image to a web service. When I resize the image to a smaller size it seems the gps information is lost. Therefore I would like to retrieve this information before resizing the image, and then send that data along with the upload.

S. Farley provides an excellent reply above about the metadata extraction library by Drew Nokes. Unfortionately I am for some reason unable to build this project on my computer (Eclipse gives me almost 400 errors with a large range of errors such as access restriction, cannot be revoled, cannot be resolved to a type, undefined and so on).

Therefore, I wonder if someone could provide me with an already android-compiled version of this library?

Anders Gardebring