Display image from database - byte[] type

Hi, how can I display on screen image stored in database? Anything from documentation didn’t help me…

public byte[] getProfileImage() {
return profileImage;
}

@Autowired
private Image<byte[]> image;
@Autowired
private InstanceContainer userDc;
[…]
UserDetails userDetails = currentAuthentication.getUser();
if (userDetails instanceof User) {
User currentUser = (User) userDetails;
image.setValueSource(new ContainerValueSource<>(userDc, “profileImage”));
}

This is only part of code I made. Saving works fine but I can’t convert it to display on screen.
I made a custom User Profile screen and my goal is to display current user’s image stored in db. Thats why I used userDetails in code.

I will appreciate any sort of help

Hi Marcin,

Use the following code to set the image content from a byte array:

@Autowired
private Image<byte[]> avatarImage;

private void showAvatar() {
    User user = (User) currentAuthentication.getUser();
    avatarImage.setSource(StreamResource.class)
            .setStreamSupplier(() -> new ByteArrayInputStream(user.getProfileImage()));
}

See Image.setSource() method description in the docs.

Also, please use triple backticks or “Preformatted text” toolbar button to format your code on the forum:
image

Regards,
Konstantin

1 Like