簡體   English   中英

Gluon JavaFX中有文件導航嗎?

[英]Is there any file navigation in Gluon JavaFX?

假設您要使用用Gluon JavaFX制作的應用程序打開圖片或文件。 有用於選擇該文件或圖片的文件導航窗口嗎?

假設我們知道我們的localRoot = /root

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

還是我需要手動將文件放置在文件夾中,然后使用表格視圖掃描所有文件並將它們放置在表格視圖中以便可以選擇它們?

您對公共存儲API的使用是錯誤的,您需要提供一個有效的名稱,例如DocumentsPictures 這些是文件系統中的有效公用文件夾。

例如,您可以獲取以下文件夾中的文件列表:

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);
    }
}

您將如何處理這些文件,或如何向用戶展示這些文件。

但是,如果要瀏覽圖像庫並將這些圖像呈現給用戶,以便他/她可以選擇一個,則應使用PicturesService::loadImageFromGallery

從設備的圖片庫中檢索圖片

這將使用本機瀏覽器應用程序,您可以在所有帶有圖片的常用文件夾中進行搜索。

它將返回一個Optional<Image> (如果用戶取消,則為空),您還可以使用getImageFile()來返回Optional<File>以及與原始圖像關聯的文件。

從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)));
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM