StorageService 在 GluonHQ 框架上出错 - 我该如何解决这个问题?

StorageService gives an error on GluonHQ framework - How do I solve this?

通常我一直在 JavaFX 中使用 FileChooser。超级容易做到这一点。只需调用它,它就会打开一个新的 window,您可以在其中 select 您的文件。完成!

但是 FileChooser 不适用于 Android 和 Iphone。我必须选择 StorageService 而不是

https://docs.gluonhq.com/charm/javadoc/5.0.1/com/gluonhq/charm/down/plugins/StorageService.html

File privateStorage = Services.get(StorageService.class)
                          .flatMap(StorageService::getPrivateStorage)
                          .orElseThrow(() -> {
                              new FileNotFoundException("Could not access private storage.");
                          });

但是这里的问题是报错了:

The method orElseThrow(Supplier<? extends X>) in the type Optional<File> is not applicable for the arguments (() -> {})

那我该如何解决呢?

你可以这样做:

Optional.empty().orElseThrow(FileNotFoundException::new);

Optional.empty().orElseThrow(()->new FileNotFoundException("Some exception"));

Optional.empty().orElseThrow(() -> {
            return new FileNotFoundException("Some exception");
        });

值得一读:When are braces optional in Java 8 lambda syntax?