Skip to content Skip to sidebar Skip to footer

Android Ndk: Why Is Aassetmanager_open Returning Null

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) {

Solution 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.

Post a Comment for "Android Ndk: Why Is Aassetmanager_open Returning Null"