从Mysql读取图像路径

时间:2013-09-21 12:38:01

标签: java imageview javafx-2

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

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]);

}
以这种方式,我无法在现场看到任何图像。有关从db读取图像和在场景中显示的任何建议吗?

我解决了问题

                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]);

1 个答案:

答案 0 :(得分:0)

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

// 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进行比较,并确认图像是否存在。

修改
如果您正在从localhost读取图像,请尝试

final Image image = images[j] = new Image("file:///" + c[j]);