jni 将 jstring 转换为 char
jni converting jstring to char
我必须将 jstring 转换为 const char。我尝试了以下。
jint
Java_flacTest_AudioFormatConversion_convertWavToFlac( JNIEnv* env, jobject thiz, jstring *wavefile, jstring *flacfile, jstring *tempfile) {
const char *wave_file = (*env)->GetStringUTFChars(env, wavefile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, wavefile, wave_file);
const char *flac_file = (*env)->GetStringUTFChars(env, flacfile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, flacfile, flac_file);
const char *temp_file = (*env)->GetStringUTFChars(env, tempfile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, tempfile, temp_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path %s\n", wave_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path 2%s\n", flac_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path3 %s\n", temp_file);
当我打印这些值时,所有值都只有 temp_file 值。我需要将所有 3 个字符串转换为 const char。我在这里做错了什么?
调用ReleaseStringUTFChars()
后GetStringUTFChars()
返回的指针无效。听起来实现恰好是 re-using 所有三个的存储——我猜指针值都是相同的。
要么使用这些值,要么使用 strdup()
复制字符串,然后再释放它们。
我必须将 jstring 转换为 const char。我尝试了以下。
jint
Java_flacTest_AudioFormatConversion_convertWavToFlac( JNIEnv* env, jobject thiz, jstring *wavefile, jstring *flacfile, jstring *tempfile) {
const char *wave_file = (*env)->GetStringUTFChars(env, wavefile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, wavefile, wave_file);
const char *flac_file = (*env)->GetStringUTFChars(env, flacfile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, flacfile, flac_file);
const char *temp_file = (*env)->GetStringUTFChars(env, tempfile, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, tempfile, temp_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path %s\n", wave_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path 2%s\n", flac_file);
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","input file path3 %s\n", temp_file);
当我打印这些值时,所有值都只有 temp_file 值。我需要将所有 3 个字符串转换为 const char。我在这里做错了什么?
调用ReleaseStringUTFChars()
后GetStringUTFChars()
返回的指针无效。听起来实现恰好是 re-using 所有三个的存储——我猜指针值都是相同的。
要么使用这些值,要么使用 strdup()
复制字符串,然后再释放它们。