Jeegar Patel
Jeegar Patel

Reputation: 27230

How to return "int array" from native method to java in NDK

see i have native function in nativeLib

 public native int [] getArrayNative();

which i am using like this

 private static int[] DEMO_NODES;

 DEMO_NODES =  nativeLib.getArrayNative();

in c code it has

JNIEXPORT jintArray JNICALL Java_com_testing_NativeLib_getArrayNative
  (JNIEnv *env, jobject obj) {

    int array[] = { 0, 1, 0, 1, 2, 1, 2, 3, 2, 3, 1, 2, 1, 2, 3, 2, 3, 1, 2 };
    jintArray temp = (*env)->NewIntArray(env,20);
    temp[0] = array[0];   // gives error
    return temp;    
}

here i want to return whole arry[] but i can not understand how to do that. here i have taken new array temp inside that tried to copy value of arry[] but it shows error. so how to do that

Upvotes: 4

Views: 1830

Answers (1)

Neet
Neet

Reputation: 4057

Use SetIntArrayRegion to fill the array, jintArray ist just some magic internal structure, nothing you can access using indices.

Prototype void SetArrayRegion(JNIEnv *env, array, jsize start, jsize len, *buf);

Upvotes: 5

Related Questions