【问题标题】:Get all files in all folders in internal and external storage获取内部和外部存储中所有文件夹中的所有文件
【发布时间】:2017-04-21 20:53:12
【问题描述】:

我正在 android studio 上开发一个项目,我需要帮助。如何在内部和外部存储的所有文件夹中按文件名搜索文件??

我尝试使用for循环在内部进行搜索。我测试了文件是否不存在,路径将是旧路径+文件夹名称。没有成功

【问题讨论】:

  • 请出示您的代码。
  • 你想做什么请详细说明,问题应该是完整和清楚的,你已经尝试过什么以及你卡在哪里

标签: android


【解决方案1】:
    public void getAllDir(File dir) {
        String pdfPattern = ".pdf or .txt or .jpg etc..";

        File listFile[] = dir.listFiles();

        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {

                if (listFile[i].isDirectory()) {
                    getAllDir(listFile[i]);
                } else {
                  if (listFile[i].getName().endsWith(pdfPattern)){
                                      //Do what ever u want

                  }
                }
            }
        }    
    }


and call this function like

getAllDir(Environment.getExternalStorageDirectory());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多