为了方便jni中使用assets文件
NDK中封装了更方便的接口直接读取assets文件
需要注意 必须在Android.mk中添加 【-landroid】
LOCAL_LDLIBS := -llog -lz
LOCAL_LDLIBS += -landroid
JNI代码如下(示例):
#include "jni_main.h"
#include <pthread.h>
#include <stdio.h>
#include <android/asset_manager_jni.h>
#include <android/asset_manager.h>
#include <string.h>
JNIEXPORT void JNICALL jni_debug_assets(JNIEnv *jenv, jobject thiz, jobject assetManager){
AAssetManager *pAsm = AAssetManager_fromJava(jenv, assetManager);
AAsset* asset = AAssetManager_open(pAsm, "web/index.html", AASSET_MODE_UNKNOWN);
if(NULL == asset){
ehome_printf("[%s]failed to read (web/index.html) \n", __FUNCTION__);
return;
}
if (NULL != asset){
off_t bufSize = AAsset_getLength(asset);
char *pBuf = (char *) malloc(bufSize + 1);
memset(pBuf, 0, bufSize + 1);
int iRealRead = AAsset_read(asset, pBuf, bufSize);
AAsset_close(asset);
ehome_printf("[%s]read size : %d\n", __FUNCTION__, iRealRead);
char text[64] = {0};
strncpy(text, pBuf, 30);
free(pBuf);
pBuf = NULL;
ehome_printf("[%s]%s\n", __FUNCTION__, text);
}
}
其中注册方法
{“jni_debug_assets”,"(Landroid/content/res/AssetManager;)V",(void *) jni_debug_assets},
java部分声明
public native void jni_debug_assets(AssetManager assetManager);
java部分调用
jniclass.jni_debug_assets(MainActivity.this.getAssets());
安卓部分目录结构
assets/web/index.html
JNI的注册和使用参考
https://blog.csdn.net/dreamInTheWorld/article/details/118631514