0

我需要从数据库中加载多个图像。为了检索图像路径,我使用以下代码,但我无法在场景中显示图像。

c[i] = m_ResultSet.getString(3);// take image path from db

for (int j = 0; j < 5; j++) {

    try {
        final Image image = images[j] = new Image(c[j]);
    } catch (NullPointerException e) {
    }
    final ImageView pic = pics[j] = new ImageView(images[j]);
    pic.setEffect(shadow);
    pic.setEffect(reflection);

    vb.getChildren().add(pics[j]);

}

这样我就看不到现场的任何图像。从数据库读取图像并在现场显示有什么建议吗?

我解决问题

                for (int j = 0; j < 14; j++) {
        files =c [j];
        File f4 = new File(files);
        try{
        final Image image = images[j] =
            new Image(f4.toURI().toString(), 256, 256, false, false);

        }
        catch(NullPointerException e ){
  }
        final ImageView pic = pics[j] =
            new ImageView(images[j]);
        pic.setEffect(shadow);
        pic.setEffect(reflection);

        vb.getChildren().add(pics[j]);
4

1 回答 1

0

据推测,类加载器无法找到给定 URL 的图像。由于您没有提供“简短、独立、正确的示例 (SSCCE)”,我现在可以为您做的唯一建议是部分引用以下 API javafx.scene.image.Image

// Example code for loading images.

import javafx.scene.image.Image;

// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);

// load an image and resize it to 100x150 without preserving its original
// aspect ratio
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// load an image and resize it to width of 100 while preserving its
// original aspect ratio, using faster filtering method
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

// load an image and resize it only in one dimension, to the height of 100 and
// the original width, without preserving original aspect ratio
// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);

将这些示例的 URL 与您在 DB 中的 URL 进行比较,并确认图像的存在。

编辑:
如果您正在从 localhost 读取图像,请尝试

final Image image = images[j] = new Image("file:///" + c[j]);
于 2013-09-21T13:28:25.183 回答