How to load image from URL
Java Image IO provides pluggable architecture for accessing images transparently.
It is simple, flexible and allows us to plugin various image readers and writers with ease.
The following example shows loading an image from URL as swing label image icon.
package com.bethecoder.tutorials.imageio;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LoadImageFromUrlTest {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
URL url = new URL ( "http://www.google.com/logos/2008/stpatricks_08.gif" ) ;
BufferedImage bufImage = ImageIO.read ( url ) ;
JFrame frame = new JFrame ( "Image loaded from URL" ) ;
JLabel label = new JLabel ( new ImageIcon ( bufImage )) ;
frame.getContentPane () .add ( label, BorderLayout.CENTER ) ;
frame.pack () ;
frame.setVisible ( true ) ;
}
}
It gives the following output,