【发布时间】:2014-09-13 08:34:37
【问题描述】:
我已经尝试了很多,但我只能得到第一张图片而不是动画 gif。
目前最好的解决方案是将动画 gif 加载到图标中,但我需要该文件。代码如下:
final URL url = new URL("http://www.freeallimages.com/wp-content/uploads/2014/09/animated-gif-images-2.gif");
JLabel l = new JLabel(new ImageIcon(url));
JOptionPane.showMessageDialog(null, l);
有人可以帮忙吗?
此代码只是获取 gif 的第一张图片(非动画):
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
File file = new File(destinationFile);
file.getParentFile().mkdirs();
file.createNewFile();
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();
}
【问题讨论】:
-
一个 URL 引用有一个
InputStream,您应该可以从中读取 -
gif 将被正确下载。一些查看器和 java 不会为 gif 设置动画。尽管通过一些努力你可以得到 java 中的所有帧。
-
这段代码应该可以工作。我认为不可能只下载“无动画”版本!你是如何显示 gif 的?
-
仅使用默认的 windows 图像查看器
-
Windows 照片查看器不会显示动画 gif。