Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java程序中存储动画GIF图像_Java_Image_Animation_Gif - Fatal编程技术网

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

在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

在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(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
并将字节数组写入其中。