【发布时间】:2012-05-07 02:34:01
【问题描述】:
我正在编写一个程序来保存来自 Internet 的图像,但有些图像最终变成了部分灰色。或者它只是真的是灰色的图标,因为如果我打开图像它根本不是灰色的。
这是我用来保存图像的方法:
public static void saveImage(String imageUrl, String destinationFile) throws IOException{
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
我认为问题在于我在图像完全写入之前关闭了流。有什么方法可以检查它是否完成或什么?
【问题讨论】:
-
代码看起来不错。 “图标”是什么意思?您可以在图像/图标完全写入之前提供它吗?你想等到这个 saveImage 方法完成。
标签: image url inputstream fileoutputstream