Prashant Kumar
Prashant Kumar

Reputation: 290

Accessing Local Resource (local directory- outside project) in Vaadin 14/RapidclipseX IMG component

How to access the local image file from my hard drive (outside project folder) in the image component (to make an image gallery)? How to write the path of the file? in Vaadin 14/RapidclipseX.

It only takes either path from the project or URL. In my project users have to upload the images and I want to make a gallery that will show the images.

I tried all the below way but didn't work:

D:\\Canavans\\Reports\\256456\\424599_320943657950832_475095338_n.jpg
D:\Canavans\Reports\256456\424599_320943657950832_475095338_n.jpg

code (Tried this way as well):

for(final File file: files)
    {
        System.out.println(file.getName());
        this.log.info(file.getName());
        this.log.info(file.getAbsolutePath());
        final String path = "file:\\\\" + file.getAbsolutePath().replace("\\", "\\\\");
        this.log.info(path);
        final Image img = new Image();
        img.setWidth("300px");
        img.setHeight("300px");
        img.setSrc("file:\\\\D:\\\\Canavans\\\\Reports\\\\256456\\\\IMG20171002142508.jpg");
        this.flexLayout.add(img);
    }

Please Help! Thanks in advance. Or is there any other way to create an image gallery?

Upvotes: 1

Views: 796

Answers (1)

Manuel Stör
Manuel Stör

Reputation: 141

Hmm as far as I know if you want to access resources outside the resource folder you can create a StreamResource and initialize the image with that. If you want to use the images "src" property then the file has to be inside one of Vaadin's resource folders (for example the 'webapp' folder).

Here is a simple example on how to create the image with a StreamResource:

final StreamResource imageResource = new StreamResource("MyResourceName", () -> {
    try
    {
        return new FileInputStream(new File("C:\\Users\\Test\\Desktop\\test.png"));
    }
    catch(final FileNotFoundException e)
    {
        e.printStackTrace();
        return null;
    }
});

this.add(new Image(imageResource, "Couldn't load image :("));

Hope this helps :)

Upvotes: 2

Related Questions