List of files in assets folder and its subfolders

后端 未结 6 1058
甜味超标
甜味超标 2020-11-27 15:00

I have some folders with HTML files in the \"assets\" folder in my Android project. I need to show these HTML files from assets\' sub-folders in a list. I already wrote some

6条回答
  •  青春惊慌失措
    2020-11-27 15:55

    Here is a solution to my problem that I found out working 100% listing all directories and files even sub-directories and files in subdirectories.

    Note: In my case

    1. Filenames had a . in them. i.e. .htm .txt etc
    2. Directorynames did not have any . in them.

      listAssetFiles2(path); // <<-- Call function where required
      
      
      //function to list files and directories
      public void listAssetFiles2 (String path){
      String [] list;
      
      try {
          list = getAssets().list(path);
          if(list.length > 0){
              for(String file : list){
                  System.out.println("File path = "+file);
      
                  if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have . 
                      System.out.println("This is a folder = "+path+"/"+file);
                      listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check 
                  }else{
                      System.out.println("This is a file = "+path+"/"+file);
                  }
              }
      
          }else{
              System.out.println("Failed Path = "+path);
              System.out.println("Check path again.");
          }
      }catch(IOException e){
          e.printStackTrace();
      }
      }//now completed
      

    Thanks

提交回复
热议问题