1

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.

toandv
  • 982
  • 13
  • 25

1 Answers1

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

Rahman
  • 3,755
  • 3
  • 26
  • 43