【问题标题】:java.lang.IllegalArgumentException: Invalid URL or resource not found. JavaFX imagejava.lang.IllegalArgumentException:无效的 URL 或找不到资源。 JavaFX 图像
【发布时间】:2016-12-04 19:32:29
【问题描述】:

我正在使用 Dropbox API V2 将图像从我的 Dropbox 下载到我的 Java 程序。我将它们保存在"/src/main/Resources/Downloads/",然后想在下载后程序运行的同时获取image.jpg

1) 程序第一次下载图像并运行时,我得到“无效的 URL 或找不到资源”错误:

Button button1 = new Button();
Image thumbnail = new Image("/Downloads/image.jpg");
button1.setGraphic(new ImageView(thumbnail));

2) 第二次关闭程序并且文件已经保存在资源中时它可以工作。

我错过了什么?

【问题讨论】:

  • 图片可能在#1调用之前/之前没有保存。
  • 你确定图片下载成功后下载功能返回吗?睡足够的时间来测试这个
  • 我刚刚做了一个 Thread.sleep(70000) 只是为了非常确定。但仍然没有成功。在我关闭程序之前,下载的图像似乎没有保存在资源文件夹中。
  • 请使用代码块 {} 而不是引号来突出显示您的代码 - 看起来会更好

标签: java image file resources dropbox


【解决方案1】:

找到解决办法:

String path = "/src/main/Resources/";

File file = new File(path + i.getLocalPathThumbnail());
String localUrl = file.toURI().toURL().toString();
Image thumbnail = new Image(localUrl, false);

【讨论】:

  • 更好的方法是使用file.toURI().toURL().toExternalForm()
猜你喜欢
  • 2019-02-02
  • 2013-06-04
  • 2015-04-13
  • 1970-01-01
  • 2014-10-25
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
相关资源
最近更新 更多