Checking if directory (folder) exists in apk via native code only

我与影子孤独终老i 提交于 2019-12-06 11:26:14

Following code allows to check if certain folder exists in apk bundle:

#include <android/asset_manager.h>

bool directoryExists(jobject assetManager, const std::string& path){
    auto assetDir = AAssetManager_openDir( assetManager, path.c_str() );
    bool r = AAssetDir_getNextFileName(assetDir) != NULL;
    AAssetDir_close( assetDir );
    return r;
}

AAsetManager_openDir will always return a pointer to initialized object, even if the specified directory doesn't exist. In other words, checking if assetDir==NULL is pointless. Trick is to check if AAssetDir_getNextFileName will return a non-null const char *. If it's NULL - there is no folder, else - there is one.

Important notice: if a folder is empty, but for some reason you need to know if it exists in apk, that code would be useless due to the way it checks folder existance. But AFAIK empty folders aren't copied to apk, so such situation is improbable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!