AAssetManager无法从共享库访问原始资源 [英] AAssetManager fails to access a raw resource from a shared library

查看:51
本文介绍了AAssetManager无法从共享库访问原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问res/raw/coolvetica.ttf中的资源.

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

这是我的Java活动类,它加载了NDK共享库:

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);
}

这是我的共享库GlyphCreator中的NDK C ++代码:

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
        }
    }
}

我已经调试了代码,并调用了getGlyphs()函数.

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

fontFile始终为NULL,我尝试从字符串中加载资源:"coolvetica.ttf", "raw/coolvetica.ttf", "res/raw/coolvetica.ttf",它们都导致fontFile为NULL.

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不为NULL,我认为获取AAssetManager的代码是正确的.

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?

感谢您的帮助.

推荐答案

仅出于作者帮助他人的意图,将其作为评论区的答案"即可: 如何在资产中获取文件从Android NDK

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

这篇关于AAssetManager无法从共享库访问原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆