List of files in assets folder and its subfolders

后端 未结 6 1051
甜味超标
甜味超标 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 16:00

    private boolean listAssetFiles(String path) {
    
        String [] list;
        try {
            list = getAssets().list(path);
            if (list.length > 0) {
                // This is a folder
                for (String file : list) {
                    if (!listAssetFiles(path + "/" + file))
                        return false;
                    else {
                        // This is a file
                        // TODO: add file name to an array list
                    }
                }
            } 
        } catch (IOException e) {
            return false;
        }
    
        return true; 
    } 
    

    Call the listAssetFiles with the root folder name of your asset folder.

        listAssetFiles("root_folder_name_in_assets");
    

    If the root folder is the asset folder then call it with

        listAssetFiles("");    
    

提交回复
热议问题