List of files in assets folder and its subfolders

后端 未结 6 1057
甜味超标
甜味超标 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:56

    i think that this is best that check file is dir or not, altarnative try,catch!

     public static  List listAssetFiles(Context c,String rootPath) {
        List files =new ArrayList<>();
        try {
            String [] Paths = c.getAssets().list(rootPath);
            if (Paths.length > 0) {
                // This is a folder
                for (String file : Paths) {
                    String path = rootPath + "/" + file;
                    if (new File(path).isDirectory())
                        files.addAll(listAssetFiles(c,path));
                    else files.add(path);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
       return files;
    }
    

提交回复
热议问题