Daniel825
(Daniel Feuser)
1
We would like to display images from a directory / File Share (FileResource).
[In Vaadin 8]
Image image = new Image ();
FileResource resource = new FileResource (“C: \ temp”);
image.setSource (resource);
In Vaadin 10 we could not find a similar function.
The component “Image” only allows access to a URL and the “frontend” folder.
how can we solve this problem?
Diego159
(Diego Sanz Villafruela)
2
Maybe this could help you:
public static Image generateImage(String imagePath) throws IOException {
return generateImage(getBytesFromFile(imagePath));
}
public static byte[] getBytesFromFile(String imagePath) throws IOException {
File file = new File(imagePath);
return Files.readAllBytes(file.toPath());
}
public static Image generateImage(byte[] bytes) {
Objects.requireNonNull(bytes)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
StreamResource sr = new StreamResource("user", () -> bis);
Image image = new Image(sr, "image");
return image;
}
bagus2
(bagus winarno)
3
Diego Sanz Villafruela:
Maybe this could help you:
public static Image generateImage(String imagePath) throws IOException {
return generateImage(getBytesFromFile(imagePath));
}
public static byte[] getBytesFromFile(String imagePath) throws IOException {
File file = new File(imagePath);
return Files.readAllBytes(file.toPath());
}
public static Image generateImage(byte[] bytes) {
Objects.requireNonNull(bytes)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
StreamResource sr = new StreamResource("user", () -> bis);
Image image = new Image(sr, "image");
return image;
}
this work well for me.
Thanks’ Very Much