ApayandAhaverst 2017-09-13 02:06 采纳率: 0%
浏览 980

如何读取母文件夹内的子文件夹和其他类型的子文件

有一个母文件夹ABC,母文件下有n个jpg图片和数个pdf文件和数个文件夹,现在想要
将所有的内容都展示出来,要如何实现
类似于如何的图片模式,里面图集的部分是图片,但无法展开
图片说明

  • 写回答

1条回答

  • 1000001000,,,, 2017-09-13 07:05
    关注

    /**

    • 获取指定目录内所有文件路径
    • @param dirPath 需要查询的文件目录
    • @param _type 查询类型,比如mp3什么的
      */
      public static JSONArray getAllFiles(String dirPath, String _type) {
      File f = new File(dirPath);
      if (!f.exists()) {//判断路径是否存在
      return null;
      }

      File[] files = f.listFiles();

      if(files==null){//判断权限
      return null;
      }

      JSONArray fileList = new JSONArray();
      for (File _file : files) {//遍历目录
      if(_file.isFile() && _file.getName().endsWith(_type)){
      String _name=_file.getName();
      String filePath = _file.getAbsolutePath();//获取文件路径
      String fileName = _file.getName().substring(0,_name.length()-4);//获取文件名
      // Log.d("LOGCAT","fileName:"+fileName);
      // Log.d("LOGCAT","filePath:"+filePath);
      try {
      JSONObject _fInfo = new JSONObject();
      _fInfo.put("name", fileName);
      _fInfo.put("path", filePath);
      fileList.put(_fInfo);
      }catch (Exception e){
      }
      } else if(_file.isDirectory()){//查询子目录
      getAllFiles(_file.getAbsolutePath(), _type);
      } else{
      }
      }
      return fileList;
      }

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作