如何从Android的Java类的字节数组传递给JNIÇNDK? [英] How to pass byte array from android java class to JNI C NDK?

查看:295
本文介绍了如何从Android的Java类的字节数组传递给JNIÇNDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java类字节数组,我想传递一个字节数组JNI C级,我不能够访问JNI C,它的数组,请帮助!

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!!!

推荐答案

您需要声明接收这样的阵列(在Java中)JNI函数:

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:

送出数据(缓冲);

,然后在C code实现这样的功能:

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

JNIEXPORT无效JNICALL Java_com_packageXXX_yourClass_sendData(JNIEnv的*
  ENV,jobject THIZ,jbyteArray数据);

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

阅读数组:

字节* CDATA = env-> GetByteArrayElements(数据,和放大器; isCopy);

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

和释放:

env-> ReleaseByteArrayElements(数据,CDATA,JNI_ABORT);

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

以上code是C ++。为了使对C工作,你需要通过JNI environement(ENV)如你调用的函数的第一个参数,像这样的:

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

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

这篇关于如何从Android的Java类的字节数组传递给JNIÇNDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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