Java ImageIO只能写入PNG,其他格式无法写入(未报告错误)
采用以下方法:Java ImageIO只能写入PNG,其他格式无法写入(未报告错误),java,javafx,Java,Javafx,采用以下方法: @FXML public void exportSideImage() { File imageLocation = ImageExport.display(); WritableImage image = SideView.getInstance().snapshot(null, null); BufferedImage bImage = SwingFXUtils.fromFXImage(image, null); try {
@FXML
public void exportSideImage() {
File imageLocation = ImageExport.display();
WritableImage image = SideView.getInstance().snapshot(null, null);
BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(bImage, getFileExtension(imageLocation), imageLocation);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getFileExtension(File file) {
String name = file.getName();
int lastIndexOf = name.lastIndexOf(".");
return name.substring(lastIndexOf + 1);
}
如果指定了“png”图像,我的程序将只写入图像。我已经在多个平台(Linux、Windows)上对此进行了测试,但问题仍然存在。有人知道这里发生了什么吗?我建议您检查ImageIO.write=>中的结果代码,它可以报告false,还可以打印文件扩展名以确认它是以下集合之一:
ImageIO.getWriterFileSuffixes()