且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

阅读在Android文件系统中的所有文件

更新时间:2022-12-16 18:20:52

不要使用/ SD卡/路径。它不能保证所有的工作时间。

Never use the /sdcard/ path. it is not guaranteed to work all the time.

使用低于code得到的路径,SD卡目录。

Use below code to get the path to sdcard directory.

File root = Environment.getExternalStorageDirectory();
String rootPath= root.getPath();

从ROOTPATH​​位置,你可以建立的路径,在SD卡上的任何文件。例如,如果有在/DCIM/Camera/a.jpg一个图像,则绝对路径将是ROOTPATH​​ +/DCIM/Camera/a.jpg

From rootPath location, you can build the path to any file on the SD Card. For example if there is an image at /DCIM/Camera/a.jpg, then absolute path would be rootPath + "/DCIM/Camera/a.jpg".

然而,列出在SD卡中的所有文件,可以使用下面的code

However to list all files in the SDCard, you can use the below code

String listOfFileNames[] = root.list(YOUR_FILTER);

listOfFileNames将拥有一切在present在SD卡上的文件名称和经过过滤器设置的条件。

listOfFileNames will have names of all the files that are present in the SD Card and pass the criteria set by filter.

假设你想列出MP3文件只,然后通过下面的过滤器类名列表()函数。

Suppose you want to list mp3 files only, then pass the below filter class name to list() function.

FilenameFilter mp3Filter = new FilenameFilter() {
File f;
    public boolean accept(File dir, String name) {

        if(name.endsWith(".mp3")){
        return true;
        }

        f = new File(dir.getAbsolutePath()+"/"+name);

        return f.isDirectory();
    }
};

词shash

Shash