JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_testJniCallbackArray(
JNIEnv* env, jobject thiz, jintArray buf, int w, int h) {
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "------testJniCallbackArray-----");

jint *cbuf;
cbuf =(*env)->NewIntArray(env, w*h); //(*env)->GetIntArrayElements(env, buf, JNI_FALSE);
if (cbuf == NULL) {//申请内存
return 0;
} int i;  //初始化数据
for(i=0;i<w*h;i++){
cbuf[i]=i;
} int alpha = 0xFF << 24;
for (i = 0; i < h; i++) {  //修改数组,
int j;
for (j = 0; j < w; j++) {
// 获得像素的颜色
int color = cbuf[w * i + j];
int red = ((color & 0x00FF0000) >> 16);
int green = ((color & 0x0000FF00) >> 8);
int blue = color & 0x000000FF;
color = (red + green + blue) / 3;
color = alpha | (color << 16) | (color << 8) | color;
cbuf[w * i + j] = color;
}
} int size = w * h;
//分配传回数组内存
jintArray result = (*env)->NewIntArray(env,size);
//设置新的值 。
(*env)->SetIntArrayRegion(env,result, 0, size, cbuf);
//(*env)->ReleaseIntArrayElements(env, buf, cbuf, 0);
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "-----------"); jclass jclazz = (*env)->GetObjectClass(env, thiz);
jmethodID methodID = (*env)->GetMethodID(env, jclazz, "jniCallback2","([I)V");//回调,将数组传回。
(*env)->CallVoidMethod(env, thiz, methodID, result); return 1;
}java里就一个简单的:
public void jniCallback2(int[] arr) {
System.out.println("jniCallback2:"+arr);
}
+然后就一堆垃圾错误了:
04-26 14:34:39.484: I/DEBUG(11094): pid: 11387, tid: 11389  >>> com.example.hellojni <<<
04-26 14:34:39.484: I/DEBUG(11094): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr ff000020
04-26 14:34:39.484: I/DEBUG(11094):  r0 00000000  r1 ff000000  r2 40009330  r3 400091e8
04-26 14:34:39.492: I/DEBUG(11094):  r4 000057af  r5 40009228  r6 40583f48  r7 aca9f60c
04-26 14:34:39.492: I/DEBUG(11094):  r8 fffffe84  r9 80000000  10 400091e8  fp 40584000
04-26 14:34:39.496: I/DEBUG(11094):  ip acaa4d48  sp 4625fd58  lr aca30910  pc aca2fc5c  cpsr a0070010
04-26 14:34:39.500: I/DEBUG(11094):  d0  00000000006835fe  d1  00650072002b9161
04-26 14:34:39.500: I/DEBUG(11094):  d2  002e00700070006c  d3  0069007400630020
04-26 14:34:39.500: I/DEBUG(11094):  d4  4428ef3c002a2890  d5  444710004428ef70
04-26 14:34:39.500: I/DEBUG(11094):  d6  00581fe044471034  d7  000000314d88280e
04-26 14:34:39.500: I/DEBUG(11094):  d8  00003d6aedea8504  d9  0000000000000000
04-26 14:34:39.500: I/DEBUG(11094):  d10 0000000000000000  d11 0000000000000000
04-26 14:34:39.500: I/DEBUG(11094):  d12 0000000000000000  d13 0000000000000000
04-26 14:34:39.500: I/DEBUG(11094):  d14 0000000000000000  d15 0000000000000000
04-26 14:34:39.500: I/DEBUG(11094):  d16 0000000000000000  d17 3fe999999999999a
04-26 14:34:39.500: I/DEBUG(11094):  d18 42eccefa43de3400  d19 3fbc71c71c71c71c
04-26 14:34:39.500: I/DEBUG(11094):  d20 4008000000000000  d21 3fd99a27ad32ddf5
04-26 14:34:39.500: I/DEBUG(11094):  d22 3fd24998d6307188  d23 3fcc7288e957b53b
04-26 14:34:39.515: I/DEBUG(11094):  d24 3fc74721cad6b0ed  d25 3fc39a09d078c69f
04-26 14:34:39.515: I/DEBUG(11094):  d26 0000000000000000  d27 0000000000000000
04-26 14:34:39.519: I/DEBUG(11094):  d28 0000000000000000  d29 0000000000000000
04-26 14:34:39.519: I/DEBUG(11094):  d30 0000000000000000  d31 0000000000000000
04-26 14:34:39.519: I/DEBUG(11094):  scr 80000010
04-26 14:34:39.609: I/DEBUG(11094):          #00  pc 0002fc5c  /system/lib/libdvm.so
04-26 14:34:39.609: I/DEBUG(11094):          #01  pc 0003090c  /system/lib/libdvm.so (dvmHeapScanMarkedObjects)
04-26 14:34:39.609: I/DEBUG(11094):          #02  pc 0001a780  /system/lib/libdvm.so (dvmCollectGarbageInternal)
04-26 14:34:39.609: I/DEBUG(11094): code around pc:
04-26 14:34:39.609: I/DEBUG(11094): aca2fc3c 0a000001 e1a01006 eb0145b1 e797c008 
04-26 14:34:39.609: I/DEBUG(11094): aca2fc4c e5961000 e59c20ac e1510002 0a0000b5 
04-26 14:34:39.609: I/DEBUG(11094): aca2fc5c e5912020 e3120101 0a000055 e3510000 
04-26 14:34:39.609: I/DEBUG(11094): aca2fc6c e1a03001 0a000021 e5950010 e1510000 
04-26 14:34:39.609: I/DEBUG(11094): aca2fc7c 3a00001e e5953000 e593c00c e5930010 
04-26 14:34:39.609: I/DEBUG(11094): code around lr:
04-26 14:34:39.609: I/DEBUG(11094): aca308f0 e58de004 e16f2f17 e59d3004 e585b014 
04-26 14:34:39.609: I/DEBUG(11094): aca30900 e1a01005 e1c77239 e0830182 ebfffcc0 
04-26 14:34:39.609: I/DEBUG(11094): aca30910 e3570000 1afffff6 e598c010 e598100c 
04-26 14:34:39.609: I/DEBUG(11094): aca30920 e061000c e1a02420 eaffffdb 0006eda8 
04-26 14:34:39.609: I/DEBUG(11094): aca30930 fffffe84 e1a03001 e59f104c e92d4030 
04-26 14:34:39.609: I/DEBUG(11094): stack:
04-26 14:34:39.609: I/DEBUG(11094):     4625fd18  0000000f  
04-26 14:34:39.609: I/DEBUG(11094):     4625fd1c  aca4e35f  /system/lib/libdvm.so
04-26 14:34:39.609: I/DEBUG(11094):     4625fd20  4428ea84  
04-26 14:34:39.609: I/DEBUG(11094):     4625fd24  46c6dfe4  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd28  aca83acb  /system/lib/libdvm.so
04-26 14:34:39.613: I/DEBUG(11094):     4625fd28  aca83acb  /system/lib/libdvm.so
04-26 14:34:39.613: I/DEBUG(11094):     4625fd2c  fffffe84  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd30  46c6dfe4  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd34  0000535c  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd38  40009228  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd3c  4053ecb8  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd40  aca9f60c  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd44  fffffe84  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd48  80000000  /system/lib/libnfc_ndef.so
04-26 14:34:39.613: I/DEBUG(11094):     4625fd4c  400091e8  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd50  df002777  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd54  e3a070ad  
04-26 14:34:39.613: I/DEBUG(11094): #00 4625fd58  000057af  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd5c  40009228  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd60  0057b000  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd64  00000000  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd68  40009304  
04-26 14:34:39.613: I/DEBUG(11094):     4625fd6c  80000000  /system/lib/libnfc_ndef.so
04-26 14:34:39.617: I/DEBUG(11094):     4625fd70  400091e8  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd74  aca30910  /system/lib/libdvm.so
04-26 14:34:39.617: I/DEBUG(11094): #01 4625fd78  acaa4d48  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd7c  40583f00  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd80  aca9f60c  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd84  400091e8  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd88  aca9f60c  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd8c  00000001  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd90  fffffe8c  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd94  fffffe84  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd98  00002710  
04-26 14:34:39.617: I/DEBUG(11094):     4625fd9c  acaa4d48  
04-26 14:34:39.617: I/DEBUG(11094):     4625fda0  00000001  
04-26 14:34:39.617: I/DEBUG(11094):     4625fda4  aca1a784  /system/lib/libdvm.so
04-26 14:34:40.222: I/BootReceiver(165): Copying /data/tombstones/tombstone_02 to DropBox (SYSTEM_TOMBSTONE)
04-26 14:34:40.234: E/InputDispatcher(165): channel '4077c518 com.example.hellojni/com.example.hellojni.HelloJni (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
04-26 14:34:40.234: E/InputDispatcher(165): channel '4077c518 com.example.hellojni/com.example.hellojni.HelloJni (server)' ~ Channel is unrecoverably broken and will be disposed!
04-26 14:34:40.234: D/Zygote(113): Process 11387 terminated by signal (11)
04-26 14:34:40.242: I/WindowManager(165): WIN DEATH: Window{4077c518 com.example.hellojni/com.example.hellojni.HelloJni paused=false}
04-26 14:34:40.242: I/ActivityManager(165): Process com.example.hellojni (pid 11387) has died.
04-26 14:34:40.269: W/InputManagerService(165): Got RemoteException sending setActive(false) notification to pid 11387 uid 10012

解决方案 »

  1.   

    jniCallback2是哪个对象的方法,与thiz是同一个?你用的可是thiz。如果不是,不实例化一个对象是不行的,你的jniCallback2()又不是静态的!
      

  2.   

    jint *cbuf;
        cbuf =(*env)->NewIntArray(env, w*h); //(*env)->GetIntArrayElements(env, buf, JNI_FALSE);
    是这里出错了,
    jintArray jints = (*env)->NewIntArray(env, w * h);
    cbuf = (*env)->GetIntArrayElements(env, jints, NULL);
    这样就可以了
    用完 再释放一次
    (*env)->DeleteLocalRef(env,jints);