如何在JNI中将UTF8字符串转换为UTF 16

时间:2013-10-19 09:52:39

标签: java-native-interface

任何人都可以告诉我在这段代码中出现了什么问题。实际上在下面的代码行中我将sdcard的路径放在jni(C代码)中的字符串中,并在连接函数中连接这些代码loop.chactate返回的字符串工作正常,但是当我将它转换为jstring时,它会在我的logcat中打印垃圾值。        请告诉我这是什么问题。

 jstring str=(jstring)env->CallObjectMethod(sdcard,storagestring);
    const char    jclass cfile=env->FindClass("java/io/File");
    jmethodID fileid=env->GetMethodID(cfile,"<init>","(Ljava/lang/String;)V");
    jclass envir=env->FindClass("android/os/Environment");
        jmethodID storageid=env->GetStaticMethodID(envir,"getExternalStorageDirectory","()Ljava/io/File;");
        jobject sdcard=env->CallStaticObjectMethod(envir,storageid);
        jclass sdc=env->GetObjectClass(sdcard);
        jmethodID storagestring=env->GetMethodID(sdc,"toString","()Ljava/lang/String;");

         *nativeString = env->GetStringUTFChars(str, 0);
     char *s =concatenate(nativeString,"/f1.3gp");
        //fpath=s;
        fpath=env->NewStringUTF(s);
    jobject fobject=env->NewObject(cfile,fileid,fpath);
    LOGI("size of char=%d size of string=%d",sizeof("/f1.3gp"),sizeof(fpath));
    jmethodID existid=env->GetMethodID(cfile,"exists","()Z");
    if(env->CallBooleanMethod(fobject,existid))
    {
     jmethodID delid=env->GetMethodID(cfile,"delete","()Z");
     if(env->CallBooleanMethod(fobject,delid))
     LOGE("File is deleting...%s",env->NewStringUTF("/f1.3gp"));
    }
    jmethodID newfileid=env->GetMethodID(cfile,"createNewFile","()Z");
    if(env->CallBooleanMethod(fobject,newfileid))
        LOGE("dig dig %s",fpath);
    jthrowable exc=env->ExceptionOccurred();
    if(exc)
    {
     env->ExceptionDescribe();
     env->ExceptionClear();
    }
    LOGE("creating file %s",fpath);
    }

0 个答案:

没有答案