从JNI传递一个数组到Java [英] Passing an Array from JNI to Java

查看:185
本文介绍了从JNI传递一个数组到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JNI访问数组到Java(创作共享库 libtest.so 文件的)结果
我做的,如下:

I want to access an array from JNI to Java (By creation of shared library libtest.so file)
I'm doing it as below:

JNI code(C)

int size = 5;                              /* Array Size */
char sample_arr[size] = {1, 2, 3, 4, 5};   /* Array to be accessed in Java */

jbyteArray result_arr = (*env)->NewByteArray(env, size);
(*env)->SetByteArrayRegion(env, result_arr, 0, size, sample_arr);

jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V");
(*env)->CallVoidMethod(env, obj, mid, get_event);

我相信这JNI是jbyte相当于字符。结果
以上code编译成一个共享对象 libtest.so

的Java code

public class Callbacks 
{
    char result_arr[];
    public void Callbacks() {
        System.loadLibrary("test");      /* Load the Shared Object */
    }

    public void callback(int get_event) 
    {
        ....
        Log.d(TAG, "Received Array at Java: result_arr[] : "+Arrays.toString(result_arr));
        ....
    }
}

输出

D/[JNI] (3106): Recieved Array at Java: result_arr[] : null

输出应为: 1,2,3,4,5 ,但我发现

这是访问数组的正确方法吗?结果
请指出任何错误。

Is this the right way of accessing the array ?
Please point out any mistake.

我是新来的Java,并在JNI初学者。熟练使用C虽然。结果
我想AP preciate有这方面的帮助。

I'm new to java, and a beginner at JNI. Proficient with C though.
I would appreciate any help on this.

推荐答案

您具体是如何从JNI发送result_arr阵列回Java?我不知道是什么get_event参数假设是,但它绝对不是result_arr参考。你应该做的两件事情1:A)添加第二个参数回调并传递result_arr回Java或B)设定从JNI的Callbacks.result_arr实例变量购买获取类,然后得到的字段ID为该类。如果您想了解更多的帮助,您将需要发布更完整的code。

How exactly did you send the result_arr array from JNI back to Java? I am not sure what the get_event argument is suppose to be, but it is definitely not a reference to result_arr. You should do 1 of two things: A) Add a second argument to the callback and pass result_arr back to Java, OR B) Set the Callbacks.result_arr instance variable from JNI buy acquiring the class and then getting the field ID for that class. If you would like more assistance, you will need to post more complete code.

这篇关于从JNI传递一个数组到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆