List assets in a subdirectory using AssetManager.list

后端 未结 9 595
梦谈多话
梦谈多话 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:05

    When you need to have access to a folder down deeper in your hierarchy use

    String[] fileNames =getAssets().list("myFolder"+File.separator+"mysubfolder");
    

    instead of "/" inside the String, which would give you an empty String array as result.

    0 讨论(0)
  • 2020-12-05 00:07

    I ve use the following code to list the file name in assets/myFolder/:

    String[] fileNames =getAssets().list("myFolder");
    for(String name:fileNames){
         System.out.println(name);    
    }
    

    note that the parameter in the method list does not contains "/".

    0 讨论(0)
  • 2020-12-05 00:15

    This solution seems working for me: Keep a copy of each file you want to access from a sub directory in the assets directory too. Use same convention. i.e. list("subdirectory")

    It doesn't read the files in assets folder, but reads all "copied" files in its sub directory. Weird!! But works!

    0 讨论(0)
  • 2020-12-05 00:16

    I'm not sure why it works this way, but when I list "/", I get root level stuff, not things from my "assets" directory. I actually found no way to properly list my assets folder in the short time I spent trying.

    The workaround I'm using is to create a "subdir" directory inside of my "assets" directory. I put all the assets that I want to access in this "subdir", and do:

    String[] assetsIWant = assetMgr.list("subdir");

    I don't know why "/" lists the "assets" directory itself as one of it's files, yet you don't have to specify the "assets" directory when giving list a path. Maybe someone else knows, but I hope this tip will help you out anyway.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:30

    There are a lot of similar questions around this topic, I suspect you are using the assets folder in a library project. if you try to access assets in a library project on android you will get empty contents and the exact symptoms you have described here

    from the android "projects" documentation

    source : http://developer.android.com/tools/projects/index.html

    Library projects cannot include raw assets

    The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.

    if this is the case, the answer is that you should only include Raw assets in the assets folder of your application project NOT in any project marked as a library (you can check this by right clicking your project and selecting properties, select the android entry on the left and look at the is library checkbox)

    0 讨论(0)
提交回复
热议问题