Android NDK: Why is AAssetManager_open returning NULL

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

I have some code:

AAsset* pAsset = AAssetManager_open(pAssetManager, "asset_test.txt", AASSET_MODE_STREAMING); DebugPrint(pAsset?"pAsset not NULL\n":"pAsset NULL"); if (pAsset) {     char buf[1024];     AAsset_read(pAsset, buf, sizeof(buf));     DebugPrint(buf);     AAsset_close(pAsset); } 

This code always prints "pAsset NULL" in logcat.

I put the asset_test.txt file in my assets directory, and I looked in the .apk to make sure it exists by renaming the .apk to .zip and opening it with 7zip.

I have some more code:

AAssetDir* pAssetDir = AAssetManager_openDir(pAssetManager, sDirectory.c_str());  if (!pAssetDir) {     DebugPrint("pAssetDir NULL\n");     return; }  const char* pszDir; while ((pszDir = AAssetDir_getNextFileName(pAssetDir)) != NULL) {     DebugPrint(pszDir); }  AAssetDir_close(pAssetDir); 

This code prints nothing. In other words, no files are ever found in the assets directory, regardless of what paths I pass into it.

Note: DebugPrint is just a prettier looking wrapper around __android_log_print().

回答1:

I passed the Activity into AAssetManager_fromJava(), while I should have passed the AssetManager in. If you pass the wrong class into AAssetManager_fromJava() it will fail without printing anything to logcat.

How to get the asset manager with JNI:

    JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();      jobject activity = (jobject)SDL_AndroidGetActivity();      jclass activity_class = env->GetObjectClass(activity);      jmethodID activity_class_getAssets = env->GetMethodID(activity_class, "getAssets", "()Landroid/content/res/AssetManager;");     jobject asset_manager = env->CallObjectMethod(activity, activity_class_getAssets); // activity.getAssets();     global_asset_manager = env->NewGlobalRef(asset_manager);      pAssetManager = AAssetManager_fromJava(env, global_asset_manager); 

Stash that asset manager pointer somewhere and use it for all your AAssetManager_*() functions from now on.



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