Download animated gif images in Java

时间:2015-11-12 11:40:22

标签: java image

I'm trying to download an animated gif image using Java, but the downloaded image loses animation. Here is my code:

    BufferedImage image = ImageIO.read(new URL("http://gifntext.com/images/football_small.gif"));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    image.flush();
    ImageIO.write(image, "gif", bufferedOutputStream);
    bufferedOutputStream.flush();

    byte[] bytes = outputStream.toByteArray();
    File file = new File("/home/toan/football_small.gif");
    FileOutputStream out = new FileOutputStream(file);

    out.write(bytes);
    out.flush();
    out.close();
    outputStream.close();
    bufferedOutputStream.close();

Anything wrong with this piece of code? Thanks in advance.

1 个答案:

答案 0 :(得分:2)

GIF is collection of frames and you have to read those frames. Try this :

http://www.java2s.com/Code/Java/2D-Graphics-GUI/ReadingagifimagewithImageIOreadanddisplayitonscreen.htm