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;
      }

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏