Greelings
Greelings

Reputation: 5434

Android : Exif of a Byte[] or Bitmap object

Is it possible to read the Exif header of a Byte[] or Bitmap object without write it on the disk? I only found one constructor ExifInterface(String filename), and it doesn't seem possible to do that. Could you please confirm?

Otherwise, how could I save that bitmap on the cache directory and take it back in order to read the Exif header?

Upvotes: 5

Views: 3764

Answers (3)

Drew Noakes
Drew Noakes

Reputation: 311255

You could use my metadata-extractor library. It has classes that'll decode Exif (and other formats) from byte[], streams, files...

Something like this should work:

Metadata metadata = new Metadata();
new ImageMetadataReader().extract(new ByteArrayReader(bytes), metadata);

Now you can inspect the Metadata object.

The lib is available via Maven.

Upvotes: 4

fantouch
fantouch

Reputation: 1169

The sample code of @Drew Noakes doesn't work for me, error occurred while reading jpg byte[] data:

GrallocMapperPassthrough: buffer descriptor with invalid usage bits 0x2080000
System.err: com.drew.imaging.tiff.TiffProcessingException: Unclear distinction between Motorola/Intel byte ordering: -40
System.err:     at com.drew.imaging.tiff.TiffReader.processTiff(TiffReader.java:60)

Then I changed to the following code and it works!!

try {
    Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(data), data.length, FileType.Jpeg);
    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            Log.d(TAG, tag.toString());
        }
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (ImageProcessingException e) {
    e.printStackTrace();
}

Upvotes: 1

greenapps
greenapps

Reputation: 11224

Bitmap have no exif header. You can save the bitmap as .png but exif is not for .png. You can save the bitmap as a .jpg file. But it will have no exif info then. But you could add it with Exifinterface i hope.

A byte array if it contains a .jpg file with an exif header would be ok. But Exifinterface is not able to read that indeed.

Upvotes: 0

Related Questions