将动画 GIF 图像存储在 Java 程序中

Store animated GIF image in a Java program

在 Java 程序中,您可以在 BufferedImage 中存储 .png、.jpg 等图像。我认为它不适用于动画 gif 图片,因为它似乎失去了动画效果。

目前我得到的正常图像如下:

BufferedImage image = ImageIO.read(new URL(images.get(x)));
String type = images.get(x).substring(images.get(x).length() - 3, images.get(x).length());
ImageIO.write(image, type, new File(title + "/" + filename));

其中 imagesString[] 个 URL。

至于 gif,我通过以下方式获取它们:

byte[] b = new byte[1];
URL url = new URL(images.get(x));
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
DataInputStream di = new DataInputStream(urlConnection.getInputStream());

FileOutputStream fo = new FileOutputStream(title + "/" + filename);
while (-1 != di.read(b, 0, 1) && downloading)           
    fo.write(b, 0, 1);
di.close();
fo.close();

但我想将它们存储在程序中,下次再将它们写入文件。如何在不将 GIF 写入文件的情况下存储 GIF 而同时保留其动画?

如果您只对将 gif 存储在内存中感兴趣,而不想让它在 java 程序中显示。您可以将收到的数据写入 ByteArrayOutputStream 而不是 FileOutputStream,然后获取生成的字节数组并稍后将其写入 FileOutputStream

如果您想显示动画 gif,您可能想查看 this post 中的顶部答案,尽管对答案的第一条评论似乎有与您类似的问题。