复制图像,丢失 Exif 数据

发布于 2024-10-10 08:29:26 字数 620 浏览 9 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2024-10-17 08:29:26

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文