-2

hello i searched a lot for a solution but nothing worked for me

    public void initialize(URL url, ResourceBundle rb) {
    Image image = new Image(IMG_PATH);
    photoAdmin.setImage(image);}

here's the IMG_PATH

    public static final String IMG_PATH = "C:\\Users\\ibeno\\Documents\\GitHub\\PiDev\\PiDev\\Images\\11880660_447255635481530_1415231414421167737_n.jpg";

and i declared photoAdmin as an ImageView but still got this error

Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
Caused by: java.net.MalformedURLException: unknown protocol: c
youngster
  • 41
  • 6

1 Answers1

-1

c is interpreted as the URL protocol.

Add file:// to your Path to fix it:

public static final String IMG_PATH = "file:///C:/Users/ibeno/Documents/GitHub/PiDev/PiDev/Images/11880660_447255635481530_1415231414421167737_n.jpg"";

But as mentioned by @James_D, this is not a very clever solution since we hardcode the value of the URL, the better way to do this would be through the Paths API:

final String IMG_PATH = "C:\\Users\\ibeno\\Documents\\GitHub\\PiDev\\PiDev\\Images\\11880660_447255635481530_1415231414421167737_n.jpg";
String pathUrl = Paths.get(IMG_PATH).toUri().toURL().toString();
Image image = new Image(pathUrl);
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • Thank you it worked When i open the page i want to show the image of current user The nameof the photo is saved in the database but when i put (IMG_PATH+ a.getPhoto); it won't show anything – youngster Aug 24 '21 at 12:40
  • I am glad it worked, please upvote and accept the answer if it helped. How are you saving the Photo in your DB. As the name of the Photo? I would suggest asking another question and putting all the details there, I will try to help you there. – Renis1235 Aug 24 '21 at 13:18
  • 1
    This is not really a robust solution though. Images are typically resources and should be bundled as part of the application. This solution will fail if the application is run on another machine, and especially another platform. URLs should never be constructed by hand like this; if you really want to read an image from the filesystem (rather than a resource), use the ~file` or `Path` API to construct the URL. – James_D Aug 24 '21 at 13:32
  • @James_D thank you for the comment, I updated the answer with your suggestions. – Renis1235 Aug 25 '21 at 06:31