How to browse all files in a specific directory?

前端 未结 1 923
别那么骄傲
别那么骄傲 2021-01-29 09:49

I\'m using Nativescript to build some app, that needs to list and present all files in the phone\'s gallery. I cannot use any \"image picker\": It just needs to present the user

相关标签:
1条回答
  • 2021-01-29 10:16

    Create an instance of Folder from the absolute path and use .getEntities() method to read list of files / folders within the particular folder.

    import { Folder } from "tns-core-modules/file-system";
    
    const androidPicturesPath = android.os.Environment.getExternalStoragePublicDirectory(
        android.os.Environment.DIRECTORY_PICTURES
    ).toString();
    
    const folder = Folder.fromPath(androidPicturesPath);
    
    folder.getEntities()
        .then((entities) => {
            // entities is an array of files and folders.
            entities.forEach((entity) => {
               console.log(entity.name);
            });
        }).catch((err) => {
            // Failed to obtain folder's contents.
            console.log(err);
        });
    

    Note: Make sure your app has READ_EXTERNAL_STORAGE permission, use nativescript-permissions plugin to acquire the permission at run time.

    0 讨论(0)
提交回复
热议问题