How to pass byte array from android java class to JNI C NDK?

后端 未结 1 364
野趣味
野趣味 2021-01-02 09:35

I have byte array in java class , and i want to pass that byte array to JNI C class, I am not able to access that array in JNI C, please help.

相关标签:
1条回答
  • 2021-01-02 10:18

    you need to declare the JNI function that receives the array like this (in Java):

    private native void sendData(byte[] data);

    you call the function like any other function:

    sendData(buffer);

    and then in your C code implement the function like this:

    JNIEXPORT void JNICALL Java_com_packageXXX_yourClass_sendData( JNIEnv* env, jobject thiz, jbyteArray data);

    read the array:

    byte * cData = env->GetByteArrayElements(data, &isCopy);

    and release:

    env->ReleaseByteArrayElements(data, cData, JNI_ABORT);

    the above code is C++. To make it work for C you need to pass the jni environement (env) as the first parameter of the function you are calling, like this:

    (*env)->GetByteArrayElements(env,...)

    0 讨论(0)
提交回复
热议问题