Omar Martinez
Omar Martinez

Reputation: 708

AAssetManager fails to access a raw resource from a shared library

I'm trying to access my resource that is in res/raw/coolvetica.ttf. enter image description here

Here is my Java activity class which loads the NDK shared library:

public class OGLActivity extends Activity{
    private GLSurfaceView m_glView;

    @Override
    public void onCreate(Bundle savedInstanceBundle) {
        super.onCreate(savedInstanceBundle);
        // some init code and setting the current view here, not neccessary for code to run
        getGlyphs(getResources().getAssets(), "Hello World");
    }

    static {
        System.loadLibrary("GlyphCreator");
    }

    public native int getGlyphs(AssetManager amgr, String text);
}

And here is my NDK C++ code inside my shared library GlyphCreator:

extern "C"
JNIEXPORT jint JNICALL Java_com_myapplication_sometest_OGLActivity_getGlyphs(JNIEnv *env, jobject object, jobject assetManager, jstring text)
{
    AAssetManager *assetMgr = AAssetManager_fromJava(env, assetManager);
    if(assetMgr != nullptr)
    {
        AAsset *fontFile = AAssetManager_open(assetMgr, "res/raw/coolvetica.ttf", AASSET_MODE_BUFFER);
        if(fontFile != nullptr)
        {
            // The code here is never reached, fontFile is always NULL
        }
    }
}

I have debugged my code and the function getGlyphs() is called.

fontFile is always NULL, I've tried loading the resource from the strings: "coolvetica.ttf", "raw/coolvetica.ttf", "res/raw/coolvetica.ttf", they all result in fontFile being NULL.

AAssetManager is not NULL and I think that the code to get the AAssetManager is correct.

Would it have anything to do with the library being shared? Does it not have access to resources?

Thanks for any help.

Upvotes: 1

Views: 417

Answers (1)

shizhen
shizhen

Reputation: 12583

Just put this as an "answer" from comments area per the author's intention for helping others: How To Get File In Assets From Android NDK

Upvotes: 1

Related Questions