Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get an image from an http address into Image object directly

 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to load an image present in an http address and put it in java.awt.image .
I used something like :

but the image is always null, even though the image exist in that url
I again tried this code:

But problem is , when we call in.available(), only a small amount of data has reached us and so only a small portion of the image is obtained (and displayed if I try to do a g.drawImage(..)).
How do I obtain the whole image data?
In short I need a code to get an image from any http site.
Thanks in advance
Tanveer
[ February 20, 2004: Message edited by: Tanveer Rameez ]
[ February 20, 2004: Message edited by: Tanveer Rameez ]
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try looking at the documentation for java.awt.Toolkit?
The Toolkit.getImage(String) method takes the name of a file. Not a url. The Toolkit.getImage(java.net.URL) method is appropriate what you are trying to do.
When you were having problems with InputStream.available(), did you try looking at the documentation?
If you did, you'd see that available() doesn't return the file size, it returns ". . . the number of bytes that can be read (or skipped over) from this input stream without blocking".
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Did you try looking at the documentation for java.awt.Toolkit?
The Toolkit.getImage(String) method takes the name of a file. Not a url. The Toolkit.getImage(java.net.URL) method is appropriate what you are trying to do.
When you were having problems with InputStream.available(), did you try looking at the documentation?
If you did, you'd see that available() doesn't return the file size, it returns ". . . the number of bytes that can be read (or skipped over) from this input stream without blocking".

Hi
Thanks for the reply. Yes, how silly of my to use a String when i should be using an url.
..silly mistake !!!
Tanveer
[ February 21, 2004: Message edited by: Tanveer Rameez ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic