Reputation: 11592
I am trying to read an image that resides somewhere on the web from my Java program. So far I have successfully loaded an image by using the following code.
URL url = new URL("http://www.google.com/images/nav_logo4.png");
Image img = Toolkit.getDefaultToolkit().getImage(url);
What I want to know is why this code (which is the first i tried) does not work:
BufferedImage img = ImageIO.read(new File("http://www.google.com/images/nav_logo4.png"));
This would have the benefit of giving me a BufferedImage
. Also, how can I make the above code block until the image is loaded? I know I can use an ImageObserver
, but is there a simpler way?
When I try the second option, I get this exception:
javax.imageio.IIOException: Can't read input file!
Upvotes: 0
Views: 5509
Reputation: 160964
A File
cannot refer to a URL.
Although I haven't tried it, there appears to be a ImageIO.read(URL)
method, which can take an URL as the input as an URL
object.
I would presume it would be called as follows:
ImageIO.read(new URL("http://url/to/my/image.png"));
Upvotes: 3