Downloading images from the web - how

Discussion in 'Java' started by Stephen Kellett, Jul 9, 2004.

  1. 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 ");
    }

    --
    Stephen Kellett
    Object Media Limited http://www.objmedia.demon.co.uk
    RSI Information: http://www.objmedia.demon.co.uk/rsi.html
    Stephen Kellett, Jul 9, 2004
    #1
    1. Advertising

  2. On Fri, 9 Jul 2004 16:36:58 +0100, Stephen Kellett wrote:

    >.However I always get an image object
    > (never get null), but it is always -1 wide and -1 high.


    <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/MediaTracker.html>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jul 9, 2004
    #2
    1. Advertising

  3. Stephen Kellett, Jul 9, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe
    Replies:
    0
    Views:
    414
  2. Joe
    Replies:
    4
    Views:
    3,984
  3. renil
    Replies:
    2
    Views:
    349
    renil
    Jul 4, 2006
  4. Joe

    downloading web images

    Joe, Oct 23, 2005, in forum: Python
    Replies:
    2
    Views:
    282
    Steven D'Aprano
    Oct 23, 2005
  5. Hans Olav Hygen

    Downloading images with python

    Hans Olav Hygen, Oct 20, 2006, in forum: Python
    Replies:
    1
    Views:
    248
    Fredrik Lundh
    Oct 20, 2006
Loading...

Share This Page