ImageIO只能写入PNG,对于其他格式失败(未报告错误)

问题描述 投票:0回答:1

使用以下方法:

  @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)上对此进行了测试,但是问题仍然存在。有人知道这是怎么回事吗?

java javafx
1个回答
0
投票

我建议您检查ImageIO.write中的结果代码,它可以报告为false,还可以打印文件扩展名以确认它是其中之一:

ImageIO.getWriterFileSuffixes()
© www.soinside.com 2019 - 2024. All rights reserved.