1

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.

Omar Martinez
  • 708
  • 6
  • 19
  • 1
    see: https://stackoverflow.com/questions/13317387/how-to-get-file-in-assets-from-android-ndk – shizhen Oct 24 '18 at 03:24
  • 1
    AssetManager doesn't provide access to resource. It provides access to data in the assets folder. Either move the file to assets, or use it as a resource. – Gabe Sechan Oct 24 '18 at 03:41
  • 1
    @GabeSechan Thank you! I have been researching the issue for about 4 hours and nowhere had i seen that "AAssetManager cannot access resources from /res". Even after looking for "how to read raw resources from NDK", AAssetManagers kept on coming up as a "solution". So If you want to leave that as an answer for people in the future I'll accept that. – Omar Martinez Oct 24 '18 at 03:51

1 Answers1

1

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

shizhen
  • 12,251
  • 9
  • 52
  • 88