在我的JavaFX应用程序中,我想保存和检索图像文件. 但我找不到解决办法. 我try 了很多种解决方案. 但我找不到解决办法. 在这些解决方案中,我可以保存图像文件,但无法在ImageView中检索和显示. 未显示错误.但我找不到解决办法. 我不知道哪里出了错. 因此,请研究我的图像保存代码和检索代码,并帮助解决这个问题. 提前谢谢你.
This is my Show Image Code个
private void showImage(String id) {
Image img = null;
try {
PreparedStatement pst = db.con.prepareStatement("select image from " + db.schema + ".tbl_user where code = ?");
pst.setString(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
Blob image = rs.getBlob("image");
InputStream imgFile = image.getBinaryStream();
img = new Image(imgFile) ;
imgFile.close();
imgView.setFitHeight(120);
imgView.setFitWidth(120);
imgView.isSmooth();
imgView.setImage(img);
lblPic.setText("");
} else {
lblPic.setText("Photo");
}
} catch (Exception e) {
Alert alt = new Alert(Alert.AlertType.ERROR,e.getMessage(),ButtonType.OK);
alt.show();
throw new RuntimeException(e);
}
}
This How I select the Image to Save.
FileChooser chooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG","*.JPEG","*.jpeg");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
chooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = chooser.showOpenDialog(stage_Emp);
if(file != null){
Image image = new Image(file.toURI().toString());
imgView.setFitHeight(120);
imgView.setFitWidth(120);
imgView.isSmooth();
imgView.setImage(image);
img_path=file.getAbsolutePath();
}
And This is How I save the Image个
FileInputStream fis = new FileInputStream(img_path);
PreparedStatement pst = db.con.prepareStatement("UPDATE "+db.schema+".tbl_user SET image=? Where code=?");
pst.setBinaryStream(1,fis,(int)img_path.length());
pst.setString(2, txtID.getText());
pst.executeUpdate();