1 回答

TA貢獻(xiàn)1773條經(jīng)驗 獲得超3個贊
您對公共存儲 API 的使用是錯誤的,您需要提供一個有效的名稱,例如Documents或Pictures。這些是文件系統(tǒng)中的有效公用文件夾。
例如,您可以獲得此類文件夾中的文件列表:
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);
}
}
您如何處理這些文件,或者如何將這些文件呈現(xiàn)給用戶,都取決于您。
但是,如果您想瀏覽圖像庫,并將這些圖像呈現(xiàn)給用戶,以便他/她可以選擇一個,您應(yīng)該使用PicturesService::loadImageFromGallery:
從設(shè)備的圖像庫中檢索圖像
這將使用本機瀏覽器應(yīng)用程序,您可以搜索所有帶有圖片的常用文件夾。
它將返回一個Optional<Image>(如果用戶取消則為空),您還可以使用getImageFile()返回Optional<File>與原始圖像關(guān)聯(lián)的文件。
來自 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)));
});
添加回答
舉報