Why is AssetManger.list() so slow?

后端 未结 4 692
遥遥无期
遥遥无期 2021-01-18 03:25

I\'m trying to populate a ListView with a mixture of files stored on the SDcard AND stored as assets in the APK. Using TraceView, I can see that the performanc

4条回答
  •  伪装坚强ぢ
    2021-01-18 04:20

    If you have a deep tree of directories in the assets you can detect firstly if an item is file or directory and then call .list() on it (really accelerates the walking through the tree). This is my solution I've discovered for this:

    try {
        AssetFileDescriptor desc = getAssets().openFd(path);  // Always throws exception: for directories and for files
        desc.close();  // Never executes
    } catch (Exception e) {
        exception_message = e.toString();
    }
    
    if (exception_message.endsWith(path)) {  // Exception for directory and for file has different message
        // Directory
    } else {
        // File
    }
    

提交回复
热议问题