Downloading images from the web - how

  • Thread starter Stephen Kellett
  • Start date
S

Stephen Kellett

Hi Folks,

I can't use the new imageio package for various reasons (mainly to do
with having to use the earlier JDKs).

So I thought this code should work. However I always get an image object
(never get null), but it is always -1 wide and -1 high. The
imageUpdate() method in the ImageObserver interface never gets notified
with anything of than -1 wide and -1 high. Does this code only work for
applets or is it meant to work for apps? If it doesn't work for apps,
how am I meant to get an image from a website using Java?

Any and all help most appreciated.

Stephen (who has been using Java for ages, but in a rather narrow field
related to blind and partially sighted people, hence my lack of
knowledge).

Image image = null;

try
{
// Create a URL for the image's location

URL url = new URL(httpURL);

// Get the image

System.out.println("Fetching image:" + httpURL);
image = java.awt.Toolkit.getDefaultToolkit().createImage(httpURL);
System.out.println("Fetched image:" + httpURL);
}
catch (MalformedURLException e)
{
System.out.println("Fetching image: BAD URL");
}
catch (IOException e)
{
System.out.println("Fetching image: I/O Error");
}

if (image == null)
{
System.out.println("Fetched null image");
}
else
{
int w, h;

w = image.getWidth(win);
h = image.getHeight(win);
System.out.println("Fetched image: " + w + " wide " + h + " high ");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top