如何浏览特定目录中的所有文件? [英] How to browse all files in a specific directory?

查看:69
本文介绍了如何浏览特定目录中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nativescript来构建一些应用程序,该应用程序需要列出并显示手机图库中的所有文件.我不能使用任何图像选择器":它只需要向用户展示所有现有图像,但可以通过应用进行显示.

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 with all existing images, but doing so through the app.

有人告诉我使用它:

android.os.Environment.DIRECTORY_PICTURES

android.os.Environment.DIRECTORY_PICTURES

但这只会返回字符串"Pictures".

But this just returns a string "Pictures".

我实际上如何访问该目录?哪些Android类处理浏览"内置目录?

How do i actually access this directory? What Android class deals with "browsing" built-in directories?

文档包含无数类.有人可以指出我正确的方向吗?

The documentation contains countless classes. Could someone point me in the correct direction?

推荐答案

从绝对路径创建Folder的实例,并使用.getEntities()方法读取特定文件夹中的文件/文件夹列表.

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

注意::确保您的应用具有 READ_EXTERNAL_STORAGE 权限,请使用

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

这篇关于如何浏览特定目录中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆