如何获得在android系统目录下的所有文件名? [英] How to get all the filenames in android directory?

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

问题描述

我试着列出在我的Andr​​oid模拟器中的特定文件夹中的所有文件和我不断收到空的答案。

Im trying to list all the files in a particular folder of my android emulator and i keep getting null answer.

我的继承人code:

File sdCardRoot = Environment.getExternalStorageDirectory();
File[] file= new File(sdCardRoot+"path");
for (File f : file.listFiles()) {
    if (f.isFile())
        String name = f.getName();

}

这似乎没有不工作知道为什么。

This doesnt seem to work dont know why.

推荐答案

我已经分裂功能两部分,第一函数获得给定路径的所有文件,第二个功能从文件阵列获取的文件名。

I've split the function in two parts, first function gets all the files in the given path and the second function gets the filenames from the file array.

public File[] GetFiles(String DirectoryPath) {
    File f = new File(DirectoryPath);
    f.mkdirs();
    File[] file = f.listFiles();
    return file;
}

public ArrayList<String> getFileNames(File[] file){
    ArrayList<String> arrayFiles = new ArrayList<String>();
     if (file.length == 0)
            return null;
        else {
            for (int i=0; i<file.length; i++) 
                arrayFiles.add(file[i].getName());
        }

    return arrayFiles;
}

这篇关于如何获得在android系统目录下的所有文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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