views:

247

answers:

1

Question: How do you programmatically distinguish between directories and regular files in the assets folder?

When using AssetManager to access files in the assets folder, it seems impossible to determine if a file is in fact a file or a directory. You get the list of files from the list method and then open the file using the open method. I thought perhaps using the openFd method to get the asset file descriptor (and then subsequently requesting the normal file descriptor) would provide me some information. But requesting the file descriptor for a directory results in an IOException (which makes sense since what would it mean for a directory to have a file descriptor...?).

Currently I'm relying on that IOException (resulting from attemptng to open a directory in the assets folder) in order to determine if a file is in fact a directory. (Opening a regular file works just fine). This seems like a bad idea. Any other suggestions to distinguish between a file and a directory?

A: 

I agree with the comments on your post. But to answer your question, make a File object pointed at the assets directory File root = new File("path/to/directory");

Then you can extract all its sub files and directories like this File[] files = root.listFiles();

Once you have the list you can determine what is what using File.isDirectory().

As for determining file types, that is as simple as using String.subString() to grab the character's following the last period.

hope this helps

mtmurdock