简体   繁体   English

来自 imageView 的 Java Fx 图像更新

[英]Java Fx image update from imageView

how to update image showing in imageView i set this image from database code to get image on imageView from db is...如何更新 imageView 中显示的图像 我从数据库代码设置此图像以从 db 获取 imageView 上的图像是...

InputStream is = resultSet.getBinaryStream("image");
OutputStream os;

os = new FileOutputStream(new File("src/sources/images/photo.jpg"));
byte[]content = new byte[1024];
int size = 0;
while((size=is.read(content))!= -1)
{
   os.write(content,0,size);
}
os.close();
is.close();
image  = new Image("file:src/sources/images/photo.jpg");

imageView.setImage(image);

image is saving in BLOB type in MySQL database图像以 BLOB 类型保存在 MySQL 数据库中

Now i want to update this image and set it again this image if i dont choose any image using file chooser please solve my this problem thanks in advance现在我想更新这个图像并再次设置它,如果我不使用文件选择器选择任何图像,请提前解决我的这个问题谢谢

File chooser code文件选择器代码

stage = (Stage) showScene.getScene().getWindow();

   file = fileChooser.showOpenDialog(stage);

   if(file != null){
               image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
       imageView.setImage(image);
       imageView.setPreserveRatio(true);
   }
  fis = new FileInputStream(file); // here i got error(null pointer exception) if i try to update withouting choosing image from filechooser 

I pass fis to the preparedstatement to update and also insert我将fis传递给Preparedstatement进行更新并插入

You are getting a NullPointerException because if you don't choose a file with the FileChooser, your file variable is set to null.您将收到NullPointerException因为如果您不使用 FileChooser 选择文件,则您的file变量将设置为 null。 This can be resolved by altering your if statement a little.这可以通过稍微改变你的 if 语句来解决。

file = fileChooser.showOpenDialog(stage);

if (file == null) {
    file = new File("path/to/default/file")
}

image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
imageView.setImage(image);
imageView.setPreserveRatio(true);

fis = new FileInputStream(file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM