如何将字节数组存储为磁盘上的映像文件? [英] How to store a byte array as an image file on disk?

查看:192
本文介绍了如何将字节数组存储为磁盘上的映像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Image的字节数组表示。如何将其作为图像文件保存在磁盘上。

I have a byte array representation of a Image. How to save it on disk as an image file.

我已经完成了这个

OutputStream out = new FileOutputStream("a.jpg");
out.write(byteArray);
out.flush();
out.close();

但是当我通过双击打开图像时,它不会显示任何图像。

But when I open the image by double-clicking it, it doesn't show any image.

推荐答案

除了未能使用try / finally块(至少在您显示的代码中)之外,应该没问题。 (顺便说一句,如果要关闭输出流,则不需要刷新输出流。)

Other than failing to use a try/finally block (at least in the code you've shown) that should be fine. (You don't need to flush an output stream if you're closing it, by the way.)

因为它不起作用,所以建议 byteArray 实际不包含JPEG编码的图像。你是如何创建 byteArray 开始的?如果它是原始表示,您可能想要对其进行编码,例如使用 javax.imageio 包。

As it's not working, that suggests byteArray doesn't actually contain a JPEG-encoded image. How have you created byteArray to start with? If it's a "raw" representation, you'll probably want to encode it, e.g. using the javax.imageio package.

这篇关于如何将字节数组存储为磁盘上的映像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆