Vaadin 11. How to load Image from imputstream

Hi,
how to load image from InputStream.

StreamResource myabstractStreamResource = … don’t know how to implement.

Image myimage = new Image( myabstractStreamResource );

Thank in advance

Best regards

This should work:

        File file = new File("img.png");
        System.out.println("Expecting to find file from " + file.getAbsolutePath());

        Image image = new Image(new StreamResource("img.png", () -> {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                // file not found
				e.printStackTrace();
            }
            return null;
        }), "alt text");

Instead of a FileInputStream, you can use any other type of InputStream as well, like a ByteArrayInputStream.

-Olli

I’m still a bit slow with the functional interface.
Thank you very much!

The same without the lambda would be like this:

        Image image = new Image(new StreamResource("img.png", new InputStreamFactory() {
            @Override
            public InputStream createInputStream() {
                try {
                    return new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }), "alt text");