javajavafxgluon-mobilejavafxports

Is there any file navigation in Gluon JavaFX?


Let's say that you are going to open a picture or a file with an application you have made with Gluon JavaFX. Is there any file navigation window to use for selecting that file or picture?

Assume that we know our localRoot = /root

File localRoot = Services.get(StorageService.class)
            .flatMap(s -> s.getPublicStorage(""))
            .orElseThrow(() -> new RuntimeException("Error retrieving private storage"));

Or do I need to place the files manually inside a folder, then use a table view to scan all files and place them inside the table view so I can select them?


Solution

  • Your use of the public storage API is wrong, you need to provide a valid name, like Documents or Pictures. These are valid public folders in your file system.

    For instance, you can get the list of files in such folder:

    File picRoot = Services.get(StorageService.class)
                .flatMap(s -> s.getPublicStorage("Pictures"))
                .orElseThrow(() -> new RuntimeException("Folder notavailable")); 
    
    File[] files = picRoot.listFiles();    
    if (files != null) {
        for (File file : files) {
            System.out.println("File: " + file);
        }
    }
    

    What you do with those files, or how do you present that to the user, is up to you.

    However, if you want to browse through a gallery of images, and present those images to the user, so he/she can pick one, you should use PicturesService::loadImageFromGallery:

    Retrieve an image from the device's gallery of images

    That will use the native browser app and you can search throughout all the usual folders with pictures.

    It will return an Optional<Image> (empty if user cancels), and you can also use getImageFile() to return Optional<File> with the file associated with the original image.

    From the JavaDoc:

    ImageView imageView = new ImageView();
    Services.get(PicturesService.class).ifPresent(service -> {
        // once selected, the image is visualized
        service.loadFromGallery().ifPresent(image -> imageView.setImage(image));
        // and the file can be shared
        service.getImageFile().ifPresent(file -> 
          Services.get(ShareService.class).ifPresent(share -> 
              share.share("image/jpeg", file)));
     });