Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying an image, loses Exif data

I am copying an image to a private directory like so:

FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();

..but when I insert it back in to Gallery, untouched, at a later time:

private void moveImageToGallery(Uri inUri) throws Exception {
    MediaStore.Images.Media.insertImage(getContentResolver(), ImageUtil.loadFullBitmap(inUri.getPath()), null, null);
}

..it apparently loses its Exif data. The rotation no longer works. Is there some way I can copy an image file and not lose that data? Thanks for any suggestions.

like image 286
Matt D. Avatar asked Nov 05 '22 04:11

Matt D.


1 Answers

FileChannel, here, seems to actually read the data, decode it, reencode it, then write it; thus losing the EXIF data. Copying a file (byte-by-byte) does not alter its content. The only thing that can happen before/after a copy is a file access change (remember: Android is based on Linux, Linux being an UNIX => rwx permissions (see chmod)), eventually denying the read or write of the file. So it is clear FileChannel does something unwanted.

This code will do the work:

InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024]; int len;
while ((len = in.read(buf)) > 0)
    out.write(buf, 0, len);
in.close();
out.close();
like image 147
ElementW Avatar answered Nov 09 '22 06:11

ElementW