Towqir Ahmed Shaem
Towqir Ahmed Shaem

Reputation: 71

Can not load image in JavaFx

I want to load image and video in JavaFX.The related part of my code is given below where video loading part is okay but Image loading part is not working.Can you give me the solution?

if (serialvalue == 1) {
    String infoquery = "select * from information where " + "categoryname like " + "'%" + selectedcategory + "%'";
    try {
        filename = getFilePathForCorrespodingSerial(infoquery, serialvalue);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("You path for video : " + filename);
    System.out.println("my choiche");
    //File path = new File("C:\\Users\\User\\Downloads\\RGACD_Directory\\arosh.jpg");
    java.io.FileInputStream fis = null;
    try {
        fis = new FileInputStream("C:\\Users\\User\\Downloads\\RGACD_Directory\\arosh.jpg");
    } catch (Exception e) {
        e.printStackTrace();
    }
    im = new ImageView(new Image(fis));
    String newpath = "C:\\Users\\User\\Downloads\\RGACD_Directory\\" + filename;
    me1 = new Media(new File(newpath).toURI().toString());
    mp1 = new MediaPlayer(me1);
    mv1.setMediaPlayer(mp1);
    mp1.setAutoPlay(true);
} 

Upvotes: 1

Views: 141

Answers (3)

Towqir Ahmed Shaem
Towqir Ahmed Shaem

Reputation: 71

Finally, this works for me:

@FXML
ImageView im;
im.setImage(new Image("file:///C:\\Users\\User\\Downloads\\arosh.png");

Upvotes: 1

lambad
lambad

Reputation: 1066

Can you give us the stack trace. Try this, put the image file in the package where your java file resides and write the code as:

ImageView icon = new ImageView(new Image(getClass().getResourceAsStream("/main/view/images/inbox.png")));

The main package is just after the src directory.

In my case, My java file is in the view package. Also note that in window we use '\\' as separators and in linux we use backslash (/).

Upvotes: 1

Aniru
Aniru

Reputation: 165

This works for me.

final ImageView im = new ImageView(
            new Image(new File("C:/Users/User/Downloads/RGACD_Directory/arosh.jpg").toURI().toString()));

Upvotes: 2

Related Questions