0

code line nr.2 is working, but I cant get code nr.1 to work. it does'nt show anything.

1) Image image = new Image("https://www.countryflags.io/no/shiny/64.png");

2) Image image = new Image("file:/Users/macbookpro/Desktop/64.png");

img1.setImage(image);

System.out.println(image.getWidth());

3
  • Are you getting any errors? Don't forget to check the Image.exception property. Commented Mar 9, 2019 at 10:12
  • no errors. only that nothing will show on my gui Commented Mar 9, 2019 at 10:29
  • Perhaps the server you're trying to take the image from does not allow hotlinking? Commented Mar 9, 2019 at 14:28

1 Answer 1

1

Have you tried to print the exception raised by the image? This is what it seems to be:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.countryflags.io/no/shiny/64.png

The getException() method contains a non-null value only if the error property is set to true and, in this case, it contains the exception which caused image loading to fail.

For further informations about 403 response code, you can give a look at this link https://httpstatuses.com/403.

Update: You can make it works simulating an access to the url through a browser, for example Firefox, in the following way:

String imgUrl = "https://www.countryflags.io/no/shiny/64.png";
URLConnection connection = new URL(imgUrl).openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
Image image = new Image(connection.getInputStream());

In this way you should be able to load the image and, in fact, image.isError() will return false.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.