Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 278
Deepak Avatar asked Jul 09 '14 09:07

Deepak


1 Answers

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,...)

like image 166
Traian Avatar answered Sep 26 '22 07:09

Traian