Image 如何在JAVAFX中加载计算机目录映像

Image 如何在JAVAFX中加载计算机目录映像,image,imageview,javafx,Image,Imageview,Javafx,我正在尝试将我的计算机文件夹图像加载到缩略图墙中。我从另一个论坛上读到,ImageView“url”实例变量不支持系统路径。我在那里尝试了解决方案,但它抛出了一个异常:java.lang.OutOfMemoryError:java堆空间,因为它一直在读取文件 另一个问题是它不断警告我使用包javafx.ext->SwingUtils.toFXImage方法 我也尝试过这样输入URL: "file://localhost//Users/USER/Pictures/Camera/test/

我正在尝试将我的计算机文件夹图像加载到缩略图墙中。我从另一个论坛上读到,
ImageView
“url”实例变量不支持系统路径。我在那里尝试了解决方案,但它抛出了一个异常:
java.lang.OutOfMemoryError:java堆空间,因为它一直在读取文件

另一个问题是它不断警告我使用包javafx.ext->
SwingUtils.toFXImage
方法

我也尝试过这样输入URL:

"file://localhost//Users/USER/Pictures/Camera/test/1.JPG"
我试图显示一些图像,但它总是只显示3到4个图像

我使用
ImageView
中给出的error函数进行了检查,它并不表示读取图像时遇到错误

还有其他选择吗

代码
你到底想做什么还不清楚。如果您正在谈论JavaFX2.0,那么下面的代码可以工作。如果要加载大量图像并需要节省内存,则只需为一次要显示的数字创建足够的ImageView即可。然后,在翻阅图像时,可以调出图像视图中包含的图像对象

public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);

    File file = new File("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    Image image = new Image(file.toURI().toString());
    ImageView iv = new ImageView(image);

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}

另一个解决方案是将InputStream传递给Image类构造函数;而且正在工作

public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);

    java.io.FileInputStream fis = new FileInputStream("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    ImageView iv = new ImageView(new Image(fis));

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}

它很简单:用浏览器打开一张图片,复制图片的整个url,并将其粘贴为图片对象的参数。不要删除“file//:”,因为这样可以加载图像。你将从那里得到你的另一个逻辑。快乐的编码

Image image = new Image("file:///C:/Users/Nigel/Desktop/my_image.png");
ImageView imgview = new ImageView(image);

很抱歉,如果我的答案来得有点晚,但这是我的方法,它可以100%的工作,无论你把你的文件 1.将图像分配到文件 2.将文件解析为URI 3.将uri.toString()分配给图像

ex代码:

File imageFile = new File("path/to/image/outside/your/jar/awesomeless.jpg");
String fileLocation = imageFile.toURI().toString();
Image fxImage = new Image(fileLocation);    
或者你可以简单地把它们放在一起,就像这样:

Image fxImage = new Image(new File("path/.../awesomemore.jpg").toURI().toString());

如果有人知道更好的方法,请告诉我们

前面的答案都不适合我。然而,这一个,我看到了一段时间,但无法找到一个原始链接,工作完美

@Override
public void start(Stage primaryStage) throws Exception {
    //create a pane to hold the image views
    Pane pane = new HBox(10);
    pane.setPadding(new Insets(5,5,5,5));

    //create the image to be used!
    Image image = new Image("/Content/vortex.jpg");

    //set some custom properties and add an image
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(100);
    imageView.setFitWidth(100);
    pane.getChildren().add(imageView);

    //add the second image view with this image and no custom properties
    pane.getChildren().add(new ImageView(image));

    ImageView imageView2 = new ImageView(image);
    imageView2.setRotate(45);
    pane.getChildren().add(imageView2);

    Scene scene = new Scene(pane, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}

关键是,在创建图像时,请以“/”开始路径(即:“/Content/vortex.jpg”)。请注意,在此设置中,根文件夹是大多数IDE中的src文件夹。

如果我要使用同一
dr
文件夹中的图像,我能否将
/imags/img.png
作为值传递给新的
文件
?这一点不清楚,您没有解释文件夹上下文是什么,或者它存在的原因,以及它相对于代码的位置。
@Override
public void start(Stage primaryStage) throws Exception {
    //create a pane to hold the image views
    Pane pane = new HBox(10);
    pane.setPadding(new Insets(5,5,5,5));

    //create the image to be used!
    Image image = new Image("/Content/vortex.jpg");

    //set some custom properties and add an image
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(100);
    imageView.setFitWidth(100);
    pane.getChildren().add(imageView);

    //add the second image view with this image and no custom properties
    pane.getChildren().add(new ImageView(image));

    ImageView imageView2 = new ImageView(image);
    imageView2.setRotate(45);
    pane.getChildren().add(imageView2);

    Scene scene = new Scene(pane, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}