我正在尝试访问资产文件夹中的子文件夹,实际上我想从中播放一个 .mp3 文件

i m trying to access subfolder in assets folder actually i want to play a .mp3 file from that

我怎样才能访问位于 * 的特定目录?我也尝试通过直接路径访问该目录,但它不起作用。

     try {
            // for assets folder add empty string
            String[] filelist = assetManager.list("");
            // for assets/subFolderInAssets add only subfolder name
            String[] filelistInSubfolder = assetManager.list("subFolderInAssets");
            String[] filelistInSubfolder1 = assetManager.list("subFolderInabcd");
            if (filelist == null) {
                // dir does not exist or is not a directory
                // *

            } else {
                for (int i=0; i<filelist.length; i++) {
                    // Get filename of file or directory
                    String filename = filelist[i];
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

试试这个。

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

如果您的文件在资产文件夹中,

listAssetFiles("");

如果您的文件在子文件夹中,

listAssetFiles("sub folder name");
  try {
            File rootFolder = new File(rootPath);
            File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains  any file,handle it like this.
            for (File file : files) {
                if (file.isDirectory()) {
                    if (getPlayList(file.getAbsolutePath()) != null) {
                        fileList.addAll(getPlayList(file.getAbsolutePath()));
                    } else {
                        break;
                    }
                } else {

                   // do your stuff here 
                }
            }
            return fileList;
        } catch (Exception e) {
            return null;
        }