如何获取文件名保存在SD卡中的android文件名、存在、android、SD

2023-09-13 00:00:20 作者:柚苏

         我在其中包含多个文件SD卡的文件夹。 现在我需要获得文件的名称。 可以任何人有任何想法如何获取文件名保存在SD卡?

i have a folder in sd card which contains several files. now i need to get the names of that files. can anybody have any idea how to get the file names stored in sd card?

任何帮助将AP preciative。 非常感谢。

any help will be appreciative. thanks a lot.

推荐答案

Environment.getExternalStorageDirectory 会给你一个文件对应的SD卡。然后,你只需要使用 文件 的方法。

Environment.getExternalStorageDirectory will give you a File corresponding to the SDCARD. Then you'll just have to use File methods.

这应该是这样的:

File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
    if (f.isFile())
        String name = f.getName();
        // make something with the name
}

一个小纸条建议:从奇巧及以上,这需要 READ_EXTERNAL_STORAG​​E 许可

A little note of advice : from KitKat and above, this requires the READ_EXTERNAL_STORAGE permission.