java报错:MalformedURLException: unknown protocol: c
1. 报错情况:
部分代码:
//打开图片
path="C:/Users/MyUser/image.jpg"
openPictrues(path);
public void openPictures(String path,String picName) throws IOException {
FXMLLoader root = new FXMLLoader(getClass().getResource("imageBar.fxml"));
AnchorPane load = root.load();
Tab tab3 = new Tab(picName);
tab3.setContent(load);
tabPane.getTabs().add(tab3);
tabPane.getSelectionModel().select(tab3);
tabPane.setVisible(true);
imageBar1 picController = root.getController();
picController.loadImage(path);
}
2. 解决办法:
需要在路径前面添加file:///
String imageUrl = "file:///C:/Users/MyUser/image.jpg";
否则就会报错,出现协议错误,具体的原因的话还不是很清楚背后的原理
//修改的关键语句
openPictrues("file:///"+path);
//打开图片
path="C:/Users/MyUser/image.jpg"
openPictrues("file:///"+path);
public void openPictures(String path,String picName) throws IOException {
FXMLLoader root = new FXMLLoader(getClass().getResource("imageBar.fxml"));
AnchorPane load = root.load();
Tab tab3 = new Tab(picName);
tab3.setContent(load);
tabPane.getTabs().add(tab3);
tabPane.getSelectionModel().select(tab3);
tabPane.setVisible(true);
imageBar1 picController = root.getController();
picController.loadImage(path);
}