5

My Android application has some files in the assets directory that I want to open on startup by listing the files in the directory and opening each. I am trying to use the AssetManager to do this but it does not seem to do as I would expect. My sample code is below. Is this the correct way or is there a better way to do this?

And I am using the following method to print out the assets directory tree.

void displayFiles (AssetManager mgr, String path) {
    try {
        String list[] = mgr.list(path);
        if (list != null)
            for (int i=0; i<list.length; ++i)
                {
                    Log.v("Assets:", path +"/"+ list[i]);
                    displayFiles(mgr, path + list[i]);
                }
    } catch (IOException e) {
        Log.v("List error:", "can't list" + path);
    }

} 

From my Activity's onCreate method I do the following:

final AssetManager mgr = getAssets();    
displayFiles(mgr, "/assets"); 
displayFiles(mgr, "./assets"); 
displayFiles(mgr, "/");
displayFiles(mgr, "./");

Which gives me the following output

09-29 20:08:27.843: DEBUG/GFlash(6543): //AndroidManifest.xml 
09-29 20:08:27.954: DEBUG/GFlash(6543): //META-INF
09-29 20:08:28.063: DEBUG/GFlash(6543): //assets
09-29 20:08:28.233: DEBUG/GFlash(6543): //classes.dex 
09-29 20:08:28.383: DEBUG/GFlash(6543): //com
09-29 20:08:28.533: DEBUG/GFlash(6543): //res
09-29 20:08:28.683: DEBUG/GFlash(6543): //resources.arsc

Thanks in advance!

John

John in MD
  • 2,141
  • 5
  • 25
  • 36

2 Answers2

13

Ugh. The problem was in the displayFiles method, it was missing the separator, "/", between the directory and the filename. Sorry if I wasted anyone's time. A corrected version of displayFiles is below.

void displayFiles (AssetManager mgr, String path) {
    try {
        String list[] = mgr.list(path);
        if (list != null)
            for (int i=0; i<list.length; ++i)
                {
                    Log.v("Assets:", path +"/"+ list[i]);
                    displayFiles(mgr, path + "/" + list[i]);
                }
    } catch (IOException e) {
        Log.v("List error:", "can't list" + path);
    }

}

John

John in MD
  • 2,141
  • 5
  • 25
  • 36
  • 1
    I tried. It tells me I cannot accept my own answer until tomorrow. – John in MD Sep 30 '09 at 13:00
  • 2
    This is showing all of the stuff in the root folder but I can't actually see any of the files in my assets folder, ever get that to work? – Nathan Schwermann Oct 07 '10 at 03:19
  • 2
    @schwiz: I had the same problem. Explanation/solution is here: http://stackoverflow.com/questions/3631370/list-assets-in-a-subdirectory-using-assetmanager-list/4295538#4295538 – Steve Blackwell Dec 09 '10 at 04:38
11

To be fully recursive you can update the method as following :

void displayFiles (AssetManager mgr, String path, int level) {

     Log.v(TAG,"enter displayFiles("+path+")");
    try {
        String list[] = mgr.list(path);
         Log.v(TAG,"L"+level+": list:"+ Arrays.asList(list));

        if (list != null)
            for (int i=0; i<list.length; ++i)
                {
                    if(level>=1){
                      displayFiles(mgr, path + "/" + list[i], level+1);
                    }else{
                         displayFiles(mgr, list[i], level+1);
                    }
                }
    } catch (IOException e) {
        Log.v(TAG,"List error: can't list" + path);
    }

}

Then call with :

final AssetManager mgr = applicationContext.getAssets();
displayFiles(mgr, "",0);     
seb.bianco
  • 111
  • 1
  • 2