{
工具类代码
public native static String getAssetStringUseJNI(Context context,String path);
1
NDK代码
#include <jni.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include "com_netease_study_task_AssetUtil.h"
/*
* Class: com_netease_study_task_AssetUtil
* Method: getAssetStringUseJNI
* Signature: (Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_netease_study_task_AssetUtil_getAssetStringUseJNI
(JNIEnv *env , jclass assetUtilClass, jobject context, jstring filename){
jclass class=(*env)->GetObjectClass(env,context);
jmethodID getAssets=(*env)->GetMethodID(env,class,"getAssets","()Landroid/content/res/AssetManager;");
//创建一个AssetManager对象
AAssetManager *mgr = AAssetManager_fromJava(env, (*env)->CallObjectMethod(env,context,getAssets));
//获取文件名并打开
jboolean iscopy;
const char *filePath = (*env)->GetStringUTFChars(env, filename, &iscopy);
AAsset* asset = AAssetManager_open(mgr, filePath, AASSET_MODE_UNKNOWN);
long size = AAsset_getLength(asset);//获取文件长度
char* buffer = (char*) malloc (sizeof(char)*size+1);
buffer[size]=‘\0‘;
AAsset_read(asset,buffer,size);
jstring result=(*env)->NewStringUTF(env,buffer);
free(buffer);
AAsset_close(asset);
return result;
};
————————————————
版权声明:本文为CSDN博主「SKY_L1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34203494/article/details/54285914
}
原文地址:https://www.cnblogs.com/YZFHKMS-X/p/12629503.html