List assets in a subdirectory using AssetManager.list

后端 未结 9 600
梦谈多话
梦谈多话 2020-12-04 23:37

My application has an assets directory in which I\'ve dumped a bunch of text files I need to load at runtime.

I have a directory full of assets of a particular type

9条回答
  •  独厮守ぢ
    2020-12-05 00:16

    I use the following two methods to ensure given asset is present:

        protected boolean hasAsset(String id) {
        return hasAsset(id,"");
    }
    
    protected boolean hasAsset(String id, String dir) {
        int idx = id.indexOf('/');
        if (idx > 0 && idx < id.length() - 1) {
            if (dir.length() > 0) {
                dir = dir + "/" + id.substring(0,idx);
            } else
                dir = id.substring(0,idx);
            return hasAsset(id.substring(idx + 1), dir);
        }
        try {
            return Arrays.asList(context.getAssets().list(dir)).contains(id);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    

提交回复
热议问题