jni send byte array to java

以你之姓@ 2022-05-31 02:51 231阅读 0赞
  1. JNIEXPORT jobjectArray JNICALL
  2. Java_ArrayHandler_returnArray
  3. (JNIEnv *env, jobject jobj){
  4. jobjectArray ret;
  5. int i;
  6. char *message[5]= {
  7. "first",
  8. "second",
  9. "third",
  10. "fourth",
  11. "fifth"};
  12. ret= (jobjectArray)env->NewObjectArray(5,
  13. env->FindClass("java/lang/String"),
  14. env->NewStringUTF(""));
  15. for(i=0;i<5;i++) {
  16. env->SetObjectArrayElement(
  17. ret,i,env->NewStringUTF(message[i]));
  18. }
  19. return(ret);
  20. }

or

  1. JNIEXPORT jintArray JNICALL
  2. Java_ArrayTest_initIntArray(JNIEnv *env, jclass cls, int size) {
  3. jintArray result;
  4. result = env->NewIntArray(size);
  5. if (result == NULL) {
  6. return NULL; /* out of memory error thrown */
  7. }
  8. int i;
  9. // fill a temp structure to use to populate the java int array
  10. jint fill[size];
  11. for (i = 0; i < size; i++) {
  12. fill[i] = 0; // put whatever logic you want to populate the values here.
  13. }
  14. // move from the temp structure to the java structure
  15. env->SetIntArrayRegion(result, 0, size, fill);
  16. return result;
  17. }

发表评论

表情:
评论列表 (有 0 条评论,231人围观)

还没有评论,来说两句吧...

相关阅读