3

Alright so I'm trying to read an Image from a URL and it's not really doing anything. Here is the method I am using to retrieve an image from a URL:

    public Image getImage(String url){
    try{
        System.out.println("gettingImage");
        return ImageIO.read(new URL(url));
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

I've added in a System.out.println() just to make sure that the method was actually executing when I called it.

The url I'm passing in to it is: http://i3.kym-cdn.com/photos/images/masonry/000/349/917/065.png (which is a valid url)

My output is this:

gettingImage

It just says 'gettingImage' (from the System.out.println() I've added) and doesn't actually get the image for some reason. There is no stacktrace, just that output. Any help would be greatly appreciated. Thanks.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • What does your function return? null? – jolivier Jul 13 '12 at 17:12
  • My function is supposed to return that image. If it returned null, I would've known due to the "e.printStackTrace()" – Josh M Jul 13 '12 at 17:13
  • How do you know that it does not get the image? Your code does not show the caller of getImage... – home Jul 13 '12 at 17:14
  • @Josh: that's wrong, please see here: http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#read(java.net.URL) – home Jul 13 '12 at 17:14
  • 1
    You should invoke that method from a new thread, since it's blocking method. Or better use [`SwingWorker`](http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html). – Eng.Fouad Jul 13 '12 at 17:15
  • I call the method inside my constructor: here is the code inside my constructor: url = "http://i3.kym-cdn.com/photos/images/masonry/000/349/919/71e.png"; image = getImage(url); ("url" and "image" are both defined with all my other variables btw) – Josh M Jul 13 '12 at 17:17
  • The posted code works without any problem here. Therefore I assume that the problem is the network connection or outside of the posted code. – Robert Jul 13 '12 at 17:19
  • I don't understand @home , I've looked at the javadoc before and I'm using it the correct way. – Josh M Jul 13 '12 at 17:19
  • @Robert That's interesting because my internet connection is more than fine, so I don't really know what is causing it NOT to get the image. – Josh M Jul 13 '12 at 17:20
  • @Josh: yes, but it still can return null without printing a stacktrace. Btw: your code perfectly works on my machine (can get the image). Did you correctly set your proxy settings? – home Jul 13 '12 at 17:22
  • @JoshM: What does your application actually do? Is it stuck and does not continue? Or does it return `null`? And please add the full code that calls `getImage` to your question. The fragment in your comment does not work: it's lacking `http://` in the URL. – Codo Jul 13 '12 at 17:30

3 Answers3

4

You should never call an intensive method that takes long time until returns from inside a main thread, this will freeze your GUI. Create a new SwingWorker and invoke that method within it. See this example.

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • @Eng: What I meant is, that I'm not sure if this is about a Swing based user interface - it could have been about a headless server-side application as well... – home Jul 13 '12 at 17:31
  • Regardless of whether or not it's threaded, it doesn't seem to retrieve the image regardless. I am aware that if it's not threaded it will temporarily freeze my GUI (due to hogging up the main thread in which the program runs in) but it would only be temporarily. I've tried putting it inside a Thread so it doesn't freeze my GUI, and it doesn't make any difference. – Josh M Jul 13 '12 at 17:32
  • 1
    @home I saw `ImageIcon` on the [OP's comment](http://stackoverflow.com/questions/11475170/imageio-readurl-taking-long/11475330#comment15151887_11475170) before he edited it :) – Eng.Fouad Jul 13 '12 at 17:33
  • @Eng: Apologies, I did not check the history - forget about my comment :-) – home Jul 13 '12 at 17:34
  • Do you guys have any ideas why it's not getting the image? – Josh M Jul 13 '12 at 17:42
  • +1 for this, even the [SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) Tutorial includes an example for this particular case :-). @JoshM, IMHO, delay in getting images from a URL can be Server side related issue like Ping issues with the server or blah blah – nIcE cOw Jul 13 '12 at 17:42
  • I'm aware @nIcEcOw but that's not the problem, regardless of whether it's threaded or not, it don't retrieve the image. – Josh M Jul 13 '12 at 17:43
3

Try this....

This program is just to show that the image is obtained, i am simply using System.out on the returned image from the url, to prove that the image is obtained.

I have also used a thread so that the process intensive work is out of the EDT (Event Dispatcher Thread) which is responsible for the UI.

Either you must use Thread or SwingWorker, else doing process intensive work on the EDT will eventually leave your GUI irresponsive...

public class UrlTest
{
    public static void main(String[] args)
    {
        new Thread (new Runnable()
        {
            @Override public void run()
            {
                try
                {
                    URL url = new URL("http://i3.kym-cdn.com/photos/images/masonry/000/349/917/065.png");
                    Image img = ImageIO.read(url);
                    System.out.println(img);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • I've somewhat changed the layout instead, I have a list full of Image URL's and basically I'm just looping through the url list and then getting the image that way, inside a thread. Here is my output to the console: http://paste.strictfp.com/29840 As you can tell, it stops getting the images after ~4, even though there are a lot more URLs. All URLs are valid URLs, but it just seems to stop getting the images. I feel like each time I run it, I keep getting different results.... I don't know what the problem is. – Josh M Jul 13 '12 at 18:17
  • Thats fine...so whats the problem now.. ?? – Kumar Vivek Mitra Jul 13 '12 at 18:19
  • It's not fine though, It stops getting the images after ~4 URLs, I don't understand. – Josh M Jul 13 '12 at 18:20
  • Is your GUI getting irreresponsive ? – Kumar Vivek Mitra Jul 13 '12 at 18:23
  • Nope, it's inside a seperate thread. It just seems that each time I run it, I'm getting different results; It's only able to get a couple of Images, but not all of them. And when it can't get an image, it doesn't even go to the next one, it just stalls there. – Josh M Jul 13 '12 at 18:24
  • So, you mean the Non-Ui separate thread is strucked after 4, or its throwing an exception...?? – Kumar Vivek Mitra Jul 13 '12 at 18:25
  • That's why I'm confused, It's NOT throwing any sort of Exception, it just stops (not the program, it just waits) and doesn't go to the next one. Alright here is the thread: http://paste.strictfp.com/29842 – Josh M Jul 13 '12 at 18:26
  • Can you pls paste the entire code in paste bin..so i can have a look, and resolve it – Kumar Vivek Mitra Jul 13 '12 at 18:27
  • The code is not even close to being finished, that's the only portion that is messing up. Just know that the 'addResults(int)' method adds URLs to the LinkedList 'urls'. By the way, there is nothing wrong with the addResults method because while it's getting the image urls, it prints them out to the screen. – Josh M Jul 13 '12 at 18:30
  • I just cant take any guess, until and unless i have a look at the code that is causing the thread to struck after 4th entry – Kumar Vivek Mitra Jul 13 '12 at 18:33
  • Seeing the rest of the code wouldn't make much of a difference anyway, that thread is placed inside my constructor, and then I start it after i initialize it. That is the only portion of the whole program that is not working, so I guess the other code would be kind of irrelevant if all methods used in the thread are working. – Josh M Jul 13 '12 at 18:35
2

Found the problem to why this was not working: It wasn't anything with my code, it was because my internet kept flickering for a split second, and it was unable to recover from that little flicker. I was unaware that my internet kept flickering, but I guess it was because when I sent it to one of my friends, and it worked perfectly on his computer, so it must be due to internet connection. Thanks for all your ideas.

Josh M
  • 11,611
  • 7
  • 39
  • 49