如何访问内部存储以列出ListView中的所有pdf文件

时间:2017-08-14 17:08:01

标签: java android

public ArrayList<String> GetFiles(String DirectoryPath) 
{
    ArrayList<String> MyFiles = new ArrayList<String>(); 
    File f = new File(DirectoryPath);
    f.mkdirs();
    File[] files = f.listFiles(); 
    if (files.length == 0) return null; 
    else {
        for (int i=0; i<files.length; i++)           MyFiles.add(files[i].getName());
    } 
    return MyFiles;
}

然后调用此函数传递Enviroment.getExternalStorageDirectory()

现在我如何为内部存储做同样的事情?

1 个答案:

答案 0 :(得分:0)

public ArrayList<String> searchPdfInExternalStorage(File folder) {
        ArrayList<String> MyFiles = new ArrayList<String>(); 
        if (folder != null) {
            if (folder.listFiles() != null) {
                for (File file : folder.listFiles()) {
                    if (file.isFile()) {
                        //.pdf files
                        if (file.getName().contains(".pdf")) {
                            MyFiles.add(file.getPath());
                            Log.d("filePath-------", "" + file.getPath());
                        }
                    } else {
                        searchPdfInExternalStorage(file);
                    }
                }
            }
        }
    }

从sd-card获取pdf文件列表。您可以在ListView上加载列表

searchPdfInExternalStorage(Environment.getExternalStorageDirectory());