简体   繁体   English

无法通过使用 javafx 存储在 mysql 中的图像路径加载图像

[英]Unable to load images by image path stored in mysql using javafx

Below here is my example code.下面是我的示例代码。 It loads images saved in a local folder by using the remaining image relative path stored in mysql, but it said unknown source..please help..它通过使用存储在 mysql 中的剩余图像相对路径加载保存在本地文件夹中的图像,但它说来源未知..请帮助..


public void LoadTimelineImg() throws SQLException {
        
    String sql = "select id, img_url from t_timeline order by id";
        
    Statement selecStatement = conn.createStatement();
        
    ResultSet resultSet = selecStatement.executeQuery(sql);
        
    while(resultSet.next()) {
        int id = resultSet.getInt("id");
        ArrayList<Image> images = new ArrayList<Image>();
        String img_path = resultSet.getString("img_url");
        images.add(new Image("\"" + "/application/images/" + img_path + "\""));

            
        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
            imageView.setImage(images.get(count));
            count++;
            if(count == 5){
                count = 0;
                }
            }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

    }
}

The Image class constructor take a string as parameter. Image class 构造函数将字符串作为参数。 This string musn't be double quoted like the one you're providing.该字符串不能像您提供的那样被双引号引起来。

So you can do the following:因此,您可以执行以下操作:

String rootPath = "/application/images/"; // or "C:/application/images/" on windows
// assuming img_path = "somefolder1/somefolder2/imagename.ext"
Image img = new Image(rootPath + img_path);

or或者

String rootPath = "/application/images/"; // or "C:/application/images/" on windows
// assuming img_path = "somefolder1/somefolder2/imagename.ext"
File file = new File(rootPath, img_path);
Image img = new Image(file.toURI().toURL().toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM