复制图像,丢失 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();
..但是当我稍后将其原封不动地插入图库时:
private void moveImageToGallery(Uri inUri) throws Exception {
MediaStore.Images.Media.insertImage(getContentResolver(), ImageUtil.loadFullBitmap(inUri.getPath()), null, null);
}
..它显然丢失了其 Exif 数据。旋转不再起作用。有什么方法可以复制图像文件而不丢失该数据吗?感谢您的任何建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FileChannel,在这里,似乎实际上是读取数据,解码它,重新编码它,然后写入它;从而丢失 EXIF 数据。复制文件(逐字节)不会改变其内容。复制之前/之后唯一可能发生的事情是文件访问更改(请记住:Android 基于 Linux,Linux 是 UNIX => rwx 权限(请参阅 chmod)),最终拒绝对该文件的读取或写入。所以很明显 FileChannel 做了一些不需要的事情。
这段代码将完成以下工作:
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: