Android: How to detect a directory in the assets folder?

前端 未结 9 1257
无人及你
无人及你 2021-01-05 18:15

I\'m retrieving files like this

String[] files = assetFiles.list(\"EngagiaDroid\"); 

How can we know whether it is a file or is a director

9条回答
  •  礼貌的吻别
    2021-01-05 18:45

    You can also try this, it works for me, since you cannot rely solely on .list()

    public static boolean isDirectory(Context context, String path) throws IOException {
        //If list returns any entries, than the path is a directory
        String[] files = context.getAssets().list(path);
        if (files != null && files.length > 0) {
            return true;
        } else {
            try {
                //If we can open a stream then the path leads to a file
                context.getAssets().open(path);
                return false;
            } catch (Exception ex) {
                //.open() throws exception if it's a directory that you're passing as a parameter
                return true;
            }
        }
    }
    

提交回复
热议问题