在Java程序中存储动画GIF图像
在Java程序中,您可以将.png、.jpg等图像存储在在Java程序中存储动画GIF图像,java,image,animation,gif,Java,Image,Animation,Gif,在Java程序中,您可以将.png、.jpg等图像存储在buffereImage中。我认为它不适用于动画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
buffereImage
中。我认为它不适用于动画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));
其中images
是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存储在内存中,而不想从java程序中显示GIF,那么如何在不将其写入文件的情况下存储GIF,而同时保留其动画?。您可以将接收到的数据写入一个而不是
文件输出流
,然后获取生成的字节数组,稍后将其写入文件输出流
如果要显示动画gif,您可能需要在,尽管关于答案的第一条评论似乎与您的问题相似。我如何将数据从
DataInputStream
写入ByteArrayOutputStream
?@codedaple您应该能够通过替换行FileOutputStream fo=new FileOutputStream(…)来完成这项工作代码>带有ByteArrayOutputStream bo=new ByteArrayOutputStream()代码>。然后,将gif写入流后,可以通过调用“toByteArray”获取字节。然后,当您想将这些字节写入文件时,只需打开一个新的FileOutputStream
并将字节数组写入其中。