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) 开发者_StackOverflowthrows 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.
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();
精彩评论