0

I need to write a program to show a picture in javafx scene, and I used ImageView to show it. But I encountered a problem.

Exception in thread "main" java.lang.IllegalArgumentException: Invalid URL: unknown protocol: f Caused by: java.net.MalformedURLException: unknown protocol: f

This is my code:

public void initialize(){
    label1.setText("success");
    imageView1.setImage(new Image("F:/a.jpg"));
}
2
  • 4
    use file:// url scheme for files Commented Aug 9, 2017 at 2:49
  • @JigarJoshi That should be an answer, not a comment. Please resist the temptation to provide correct, complete answers in comments. Commented Aug 9, 2017 at 3:02

2 Answers 2

1

Try anyone code of block. I hope it would resolve your error.

final imageView1 imv = new imageView1();
        final Image image2 = new Image(Main.class.getResourceAsStream("a.jpg"));
        imv.setImage(image2);

or

    @FXML
    private ImageView1 imageView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        File file = new File("F:/a.jpg");
        Image image = new Image(file.toURI().toString());
        imageView.setImage(image);
    }

Or

FileInputStream input = new FileInputStream("F:/a.jpg");
Image image = new Image(input);
ImageView1 imageView = new ImageView1(image);
Sign up to request clarification or add additional context in comments.

2 Comments

I use the second way and solve this error, and thank you!
First one wouldn't work unless the image file was moved into the same package as Main.
1

A filename is not a URL. A URL is a URL. This URL should read "file:/F:/a.jpg".

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.