简体   繁体   English

如何读取设备中的所有文件和目录?

[英]How can I read all files and directory in a device?

Is it possible to develop a Java application that can read all files and directories in the device's memory?是否可以开发一个Java应用程序,可以读取设备的memory中的所有文件和目录?

I think it's not possible (for security reasons), but I need another opinion.我认为这是不可能的(出于安全原因),但我需要另一种意见。

You can do it like this.你可以这样做。

I will take path is the device storage.我将采取的路径是设备存储。

If you are using Android Studio or a likewise tool, put the debug point next to:如果您使用的是Android Studio或类似工具,请将调试点放在:

 File[] dir = new File(path).listFiles();

And you will see the file/folder hierarchy.您将看到文件/文件夹层次结构。

path = Environment.getExternalStorageDirectory().toString()

public static ArrayList<String> listFoldersAndFilesFromSDCard(String path) {
    ArrayList<String> arrayListFolders = new ArrayList<String>();

    try {
        File[] dir = new File(path).listFiles();
        // Here 'dir' will give you a list of folder/files in the hierarchy

        if (null != dir && dir.length > 0) {
            for (int i = 0; i < dir.length; i++) {
                if (dir[i].isDirectory()) {
                    arrayListFolders.add(new File(dir[i].toString()).getName());
                    // Here you can call recursively this
                    // function for file/folder hierarchy
                }
                else {
                    // Do whatever you want with the files
                }
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return arrayListFolders;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用NDK读取有根设备的系统目录中的文件? - How can I read files in system directory on a rooted device using NDK? 如何从android上的usb设备读取文件? - How can I read files from usb device on android? 如何列出和选择设备中的所有音频文件? - how can i list and choose all audio files in the device? 如何授予超级用户对fileinputstream对象的权限并读取我的已root用户设备上的所有系统文件 - How do I grant super user permission to fileinputstream object and read all system files on my rooted device 如何读取目录和文件 - How to read directory and files 如何在不使用计算机的情况下读取设备上的数据库文件 - How can I read my db files on my device, without using a computer 如何读取我的BLE设备的所有特性值? - How can read all characteristic's value of my BLE device? 我怎样才能获得SD卡上所有mp3文件的列表,无论android中的目录如何? - How can I get a list of all mp3 files on the sd card regardless of directory in android? 如何使用 Medistore (Android 10/Q) 列出目录中的所有文件? - How can I list all the files in directory using Medistore (Android 10/Q)? 如何列出 Android 目录中的文件? - How can I list files in a directory in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM