复制图像会丢失Exif数据

时间:2011-01-04 02:17:37

标签: android image exif

我正在将图像复制到私人目录,如下所示:

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();

..但是当我把它重新插入到Gallery中时,以后不再触及:

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

..它显然失去了它的Exif数据。轮换不再有效。有什么方法可以复制图像文件而不会丢失数据吗?感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

FileChannel,在这里,似乎实际读取数据,解码,重新编码,然后写入;因此失去了EXIF数据。复制文件(逐个字节)不会改变其内容。复制之前/之后唯一可能发生的事情是文件访问更改(请记住:Android基于Linux,Linux是UNIX => rwx权限(请参阅chmod)),最终拒绝读取或写入的文件。所以很明显FileChannel会做一些不必要的事情。

此代码将完成工作:

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();
相关问题