Android NDK:从共享库中的资产读取文件 [英] Android NDK: read file from assets inside of shared library
问题描述
在我的应用中,我必须将文件从资产文件夹传递到共享库. 我现在无法使用jni做到这一点. 我在项目中使用了预编译的共享库,其中已对文件的路径进行了硬编码,但出现错误无此文件或目录" . 因此,在我的 .apk 文件中,我的 libs/armeabi-v7a 文件夹中有 .so 文件,而我的文件在/assets 中em>文件夹.
In my app I have to pass a file from assets folder to shared library. I cannot do it with use of jni right now. I'm using precompiled shared library in my project, in which I have hardcoded path to my file, but I'm getting error "No such file or directory". So in my .apk file I have .so file in libs/armeabi-v7a folder and my file in /assets folder.
我试图这样做:
char *cert_file = "/assets/cacert.cert";
av_strdup(cert_file);
和其他一些路径,但这不起作用.
And some other paths, but it doesn't work.
有可能吗?
推荐答案
您可以简单地在C ++中使用AAssetManager类.
You can simply use the AAssetManager class in C++.
基本上,您需要:
- 在库初始化期间,获得一个指向以下位置的指针:
AAssetManager* assetManager
-
使用它来读取文件:
- During the init of you library get a pointer on:
AAssetManager* assetManager
Use it to read your file:
// Open your file
AAsset* file = AAssetManager_open(assetManager, filePath, AASSET_MODE_BUFFER);
// Get the file length
size_t fileLength = AAsset_getLength(file);
// Allocate memory to read your file
char* fileContent = new char[fileLength+1];
// Read your file
AAsset_read(file, fileContent, fileLength);
// For safety you can add a 0 terminating character at the end of your file ...
fileContent[fileLength] = '\0';
// Do whatever you want with the content of the file
// Free the memoery you allocated earlier
delete [] fileContent;
您可以在此处找到官方ndk文档.
要获取AAssetManager对象:
To get the AAssetManager object:
- 在本机活动中,您主要充当参数 android_app *应用,您只需要在此处获取它:app-> activity-> assetManager
- 如果您有Java活动,则需要通过JNI向其发送Java对象AssetManager的实例,然后使用功能
- In a native activity, you main function as a paramater android_app* app, you just need to get it here: app->activity->assetManager
- If you have a Java activity, you need so send throught JNI an instance of the java object AssetManager and then use the function AAssetManager_fromJava()
这篇关于Android NDK:从共享库中的资产读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!