List assets in a subdirectory using AssetManager.list

Passing an empty string seems to work for me. I get a list of the files in my assets directory when I do the following:

AssetManager aMan = appContext.getAssets();
String[] filelist = aMan.list("");

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 "/".


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.


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.


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)