0

I cannot convert jstring to char array

JNIEXPORT jlong JNICALL Java_com_jnitest_JNI_getUid(JNIEnv * env,jobject thiz,jstring user){
const char *userName = (*env)->GetStringUTFChars(env,user,0);
(*env)->ReleaseStringUTFChars(env,user,userName);
FILE * f = fopen("/data/data/com.jnitest/p.txt","w");
if(f != NULL){
    fprintf(f,"%c%c%c",userName[0],userName[1],userName[2]);
fclose(f);
}
return getUser(userName);
}

And in file p.txt i have this: ��

1 Answer 1

1

I have a mistake:

I'm releasing string before printing it to file.

This code:

(*env)->ReleaseStringUTFChars(env,user,userName);

should be at end of function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.