zoukankan      html  css  js  c++  java
  • NDK,在JNI层使用AssetManager读取文件

    NDK,二进制文件数据读取,在JNI层,通过AAssetManager读取asset内部的资源:

      需要头文件的支持
      #include <android/asset_manager_jni.h>
      #include <android/asset_manager.h>

    1,首先传个AssetManager到JNI层;
        AssetManager assetManager = getAssets();
    2,将你的数据放到assets文件夹中,然后和对应的文件名字一起,通过JNI Native函数传递到JNI:
        readFromAssets(assetManager, "yourdata.bin");
    3,然后在JNI的Native实现函数中读取:(也可直接在对应的C函数调用,调用方法类似fopen,fread)
    
    JNIEXPORT  jstring JNICALL Java_com_lib_MyLib_readFromAssets(JNIEnv* env, jclass clazz,
            jobject assetManager, jstring dataFileName) {
            
        AAssetManager* mManeger = AAssetManager_fromJava(env, assetManager);
        jboolean iscopy;
        const char *dataFile = env->GetStringUTFChars(dataFileName, &iscopy);
        
        int c = dataRead(mManeger, dataFile);  //call the C function
    
        env->ReleaseStringUTFChars(dataFileName, dataFile);
        
        jstring resultStr;
        resultStr = env->NewStringUTF("success");
        return resultStr;
    }
    int dataRead(AAssetManager* mManeger, const char *dataFile){
    
        AAsset* dataAsset = AAssetManager_open(mManeger, dataFile, AASSET_MODE_UNKNOWN);//get file read AAsset
        off_t dataBufferSize = AAsset_getLength(dataAsset);
        
        int num = dataBufferSize/sizeof(float);  
        
        //float *data = (float*) malloc(num * sizeof(float));  //allocate the data, the same with the later line
        float *data = (float*) malloc(dataBufferSize);
        
        int numBytesRead = AAsset_read(dataAsset, data, dataBufferSize);  //begin to read data once time
      //note: numBytesRead is the total bytes, then num = dataBufferSize/sizeof(float) = numBytesRead/sizeof(float)
    if (numBytesRead<0) { LOGI("read data failed"); } else{ LOGI("numBytesRead: %d", numBytesRead); } //int numBytesRead; //for (int i = 0; i < num; i++) { // numBytesRead = AAsset_read(dataAsset, (char*) (&data[i]), sizeof(float)); //or read the data one by one // if (numBytesRead<0) { // LOGI("read data failed"); // } // else{ // LOGI("numBytesRead: %d", numBytesRead); // } //} AAsset_close(dataAsset); free(data); return 0; }
  • 相关阅读:
    WordPress Export To Text插件‘download’参数远程文件包含漏洞
    WordPress EELV Newsletter插件跨站脚本漏洞
    WordPress Exploit Scanner插件安全绕过漏洞
    WordPress Feedweb插件跨站脚本漏洞
    WordPress User Role Editor插件跨站请求伪造漏洞
    Apache Struts ‘includeParams’安全绕过漏洞
    Linux Kernel ‘tcp_collapse()’ 本地拒绝服务漏洞
    Linux Kernel "iscsi_add_notunderstood_response()"缓冲区溢出漏洞
    开始读《Agile Web Development with Rails》
    开始使用2个显示器
  • 原文地址:https://www.cnblogs.com/hansjorn/p/4873824.html
Copyright © 2011-2022 走看看