0

I am trying to list all the files in an assets subdirectory (including sub-subdirectories) but when it comes to identify if a file is a regular file or directory I got stuck.

The directory tree can look like this:

assets/imports
assets/imports/set_one/...(multiple files)
assets/imports/set_two/...(multiple files)
assets/imports/...(other optional files and directories)

What I've tried so far is here:

val IMPORTS_PATH = "imports"
val assetFileNames = context.assets.list(path)!!.asList()
for(file in assetFileNames) {
    val subPathString = "$IMPORTS_PATH/$file"
    val isDir1 = File(subPathString).isDirectory //always false,
    val isFile1 = File(subPathString).isFile //always false, same as above
    
    val subPathObj = Paths.get(subPathString)
    val isDir2 = Files.isDirectory(subPathObj) //always false, same as above

I've even tried to read file attributes but got NoSuchFileException although the file does exist.

Is there a clean way to tel if a file is a directory or just regular file?

shadox
  • 3,238
  • 4
  • 24
  • 38
  • 1
    Assets are files only on your development machine. They are entries in a ZIP-style archive on the device (your APK file). You cannot use `File` or `Files` with Android assets on the device. – CommonsWare Apr 22 '23 at 19:58
  • Not sure I get what "Assets are files only on your development machine" means. They are still files and part of the APK. If I can't use File or Paths or any other Java style NIO/IO is fine but what's the best way to tell the difference? – shadox Apr 22 '23 at 20:02
  • "They are still files" -- they are not files on the phone. – CommonsWare Apr 22 '23 at 20:08
  • Ok, got what you meant. Now it's clear why NIO and IO does not work. I've checked other Stackoverflow questions and they are pretty much hacks. Well, I guess I'll have to adopt one of those solutions. – shadox Apr 22 '23 at 20:44
  • Tbh I thought that the API can read inside the ZIP... I guess I'm asking too much. – shadox Apr 22 '23 at 20:46
  • 1
    If you can open an input stream for the entry its a file. Else a directory. – blackapps Apr 22 '23 at 20:46
  • "I thought that the API can read inside the ZIP" -- it does. The ZIP format does not really have the notion of "directories". ZIP entries have names, and those names can have separators like `/` that could be considered directories. The assets API basically maps directly to how ZIP files work. – CommonsWare Apr 22 '23 at 20:50
  • I meant IO/NIO api not assets api. Anyway, point taken. It all makes sense now. – shadox Apr 24 '23 at 11:12

0 Answers0