【问题标题】:Java Fx image update from imageView从 imageView 更新 Java Fx 图像
【发布时间】:2019-05-06 23:53:53
【问题描述】:

如何更新 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);

图像在 MySQL 数据库中以 BLOB 类型保存

如果我不使用文件选择器选择任何图像,现在我想更新此图像并再次设置此图像 请解决我的这个问题 在此先感谢

文件选择器代码

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 

我将 fis 传递给准备好的语句以更新并插入

【问题讨论】:

  • 问题不清楚:代码中没有文件选择器,因此您始终不会根据提供的信息选择文件。此外,是什么阻止您再次执行此代码来进行更新?
  • 我没有给出文件选择器的代码,因为我不认为这是必需的
  • 问题是如何将此图像设置为数据库....好吧,我尝试解释更多

标签: java mysql javafx imageview blob


【解决方案1】:

您将获得NullPointerException,因为如果您不使用 FileChooser 选择文件,您的file 变量将设置为空。这可以通过稍微改变你的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多