Question

I've been struggling with a simple assets directory listing. I have the following structure (roughly speaking):

-dirA
--data.xml
--AudioSUBdir
---some files here
--VideoSUBdir
---some files here
-dirB
--data.xml
--AudioSUBdir
---some files here
-test    
--SUBtestDir

All I want is a listing of the parent asset directory (i.e. dirA, dirB, and test; I will then read the data.xml files and on basis of this do something with the content of the subdirs).

I thought that getting the desired two strings would be easy:

try {
   String[] fileNames;
   fileNames = getAssets().list("");
   for(String name:fileNames){
      // process it, e.g. write it out }
} catch (IOException e) {}

However, what I'm getting is something like AudioSUBdir, dirA, dirB, VideoSUBdir, webkit (there ain't no damned webkit at all!) It seems to be doing several weird things at the same time:

  • it ignores the directory structure, i.e. reads even subdirectories where it shouldn't
  • it magically displays a webkit directory although there is none whatsoever in the entire project folder
  • it ignores the "test" directory completely

So my questions are two (and a half)

  1. how do I get just the root folders (I want to later test them for existence of the file folder/data.xml, which will be processed and the application will do whatever it's supposed to)
  2. any explanation of what is going on. I could theoretically deal with the fact that it even writes out subdirs although I don't want them. I could even get over the fact that it ignores the test folder (perhaps because they are empty? - Well empty, containing just a subdir, not a file, but anyway...) But where the hell is the webkit folder coming from? Is it somehow "post-generated" by the apk buidling process?
  3. I'm getting a feeling that all subfolders are actually collapsed into one root. Does it mean that files of the same name will collide during the building process?
Was it helpful?

Solution

As far as the 1st (and major) issue of my question is concerned, I think I've figured it out.

try {
   String[] fileNames;
   fileNames = MyAudioGuideApplication.this.getAssets().list("");
   for(String name:fileNames){
       if (Arrays.asList(getResources().getAssets().list(name)).contains("data.xml"))
        process.the.file.here
   }
} catch (IOException e) {}

I'm still not sure how exactly getAssets works but at least the following seems to turn out:

  • folders are ignored if they contain no files
  • output is sorted alphabetically regardless the structure
  • when called from the root folder - i.e. ...list("") - it lists even subdirectories

Maybe it can help someone struggling as I have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top