I am rewriting my question because I am struggle to find this solution. I've made a simple form to register some basic information about a student you know name, last name and so on. It also has an option to choose a photo by filechooser class to find a picture from the file system.
Let's take a look at this piece of code.
@FXML
public void onBrowserPhoto() throws IOException {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
is = new FileInputStream(file);
Image image = new Image(is);
imageView.setImage(image);
}
}
At this point when the user clicks the button they will choose a picture then I created a new fileInputStream and reference that is variable which is an inputream object.
private void businessOnSave() {
if (currentStudent.getId() == 0) {
try {
currentStudent.setIs(is);
currentStudent
.setGenger(buttonMale.isSelected() == true ? buttonMale.getText() : buttonFemale.getText());
if (!checkStudent(currentStudent)) {
service.saveStudent(currentStudent);
studentUnBind(currentStudent);
sweep();
listView.getItems().add(currentStudent.getNickname());
buttonNew.disableProperty().setValue(false);
is.close();
} else {
return;
}
} catch (ServiceException | IOException e) {
e.printStackTrace();
}
}
When the use clicks on the save buttom this method will be called and the currentStudent variable receives that inputream (is) next it will be doing some stuff then I save it with service object. Everithing is perfect the student obj is saved.
Here is the method which save a new student
@Override
public void save(Student t) throws SQLException {
try (PreparedStatement ps = con.prepareStatement(INSERT)) {
ps.setString(1, t.getNickname());
ps.setString(2, t.getLastName());
ps.setString(3, t.getEmail());
ps.setString(4, t.getPhone());
ps.setString(5, t.getGenger());
ps.setDate(6, Date.valueOf(t.getBirthDate()));
ps.setBinaryStream(7, t.getIs());
ps.executeUpdate();
}
}
As you can see I am passing that inpustream variable to a preparedStatement object and it is ok, I confirm in the database the data is there. but when I bring this record from database it doesn't show the image on the UI.
here is the method that return a student record from that base.
public Student getStudentByName(String name) throws SQLException {
Student student = new Student();
try (PreparedStatement ps = con.prepareStatement(GET_STUDENT_BY_NAME)) {
ps.setString(1, name);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
student.setId(rs.getInt("student_id"));
student.setNickname(rs.getString("nickname"));
student.setLastName(rs.getString("lastname"));
student.setEmail(rs.getString("email"));
student.setPhone(rs.getString("phone"));
student.setGenger(rs.getString("genger"));
student.setBirthDate(rs.getDate("birthdate").toLocalDate());
try(InputStream is = rs.getBinaryStream("photo")){
BufferedImage bg = ImageIO.read(is);
if(bg != null){
Image image = SwingFXUtils.toFXImage(bg, null);
student.setPhoto(image);
}
} catch (IOException e) {
e.printStackTrace(); }
}
}
}
return student;
}
The bg objec is null and i have no clue why because on database the image bytes is there. I have made another way to get the bytes then take care of where the student objet is created but it didn't work as well. If someone know a solution to this problem i will be happy. Thank you for taking the time to reading this question.
Images are binary data - this is easily represented as byte arrays. The image in the sample is stored in the database as a BLOB - not a string or location, that is, it is binary data.
In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file. Example: Java.
You can try to generate an image from the byte array and check for the ArgumentException if its not. Show activity on this post. Most other types of image files have similar magick numbers. But checking that won't actually tell you if the file is a valid image file.
To draw image, first convert a byte array into Image using the following code:
Image img = new Image(new ByteArrayInputStream(buffer));
Then use the drawImage
method of graphicsContext
:
graphicsContext.drawImage(img, x, y, width, heighth);
So my suggestion is not to store Image in the student object.
You can use byte array property and when you are displaying student record convert into Image as above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With