-1

I have tried a lot but, all i can get is just the first image and not the animated gif.

The best solution at the moment was load an animated gif into icon, but i need the file. Here the code:

final URL url = new URL("http://www.freeallimages.com/wp-content/uploads/2014/09/animated-gif-images-2.gif");

JLabel l = new JLabel(new ImageIcon(url));
JOptionPane.showMessageDialog(null, l);

Can anyone help here?

This code just get the first image of gif (not animated):

public static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    File file = new File(destinationFile);
    file.getParentFile().mkdirs();
    file.createNewFile();
    OutputStream os = new FileOutputStream(destinationFile );

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}
Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47
user123453453
  • 38
  • 1
  • 3

1 Answers1

0

As far as the code is concerned, it is fine and the GIF should get downloaded properly.(Compare sizes of the original and downloaded GIF for confirmation). Now there are two points:

1. Windows Photo Viewer can not display animated GiF's, use Internet Explorer instead of it.
2. In java do not use Event-Dispatcher thread to display any animated GIF, it will not animate for sure. Try using another thread for displaying any frame containing animated GIF.

As is clear from your code, you have initialized a dialog from JOptionPane soon after initializing a JLabel with animated GIF, don't do this. All dialogs in JOptionPane are modal which will block your current thread execution and therefore you will not see any animation.