如何使用JNI jobjectarray(Java String [])填充C ++ char []?

时间:2016-07-19 11:18:19

标签: java android c++ android-ndk java-native-interface

我认为问题就是这么说的。我使用的是Android NDK。我不能包括std,请不要使用矢量,只需简单易懂的c ++。 这就是我到目前为止所拥有的:

// filePaths = jobjectarray = Java String[]
int elementCount = env->GetArrayLength(filePaths);

// this should end up being the char[] with the filePaths in it
char *cppFilePaths[elementCount];

for (int i = 0; i < elementCount; i++) {
  jstring jFilePath = (jstring) (env->GetObjectArrayElement(filePaths, i));
  const char *cppFilePath = env->GetStringUTFChars(jFilePath, 0);

  // this does not work!
  cppFilePaths[i] = cppFilePath;

  env->ReleaseStringUTFChars(jFilePath, cppFilePath);
  env->DeleteLocalRef(jFilePath);
}

使用此代码,我最终将cppFilePaths包含elementCount中最后一个字符串的filePaths个条目。

我搜索了很多,发现了strcpymemcpy,但到目前为止没有任何效果。

1 个答案:

答案 0 :(得分:0)

现在可以使用了。我不知道,如果可以直接使用GetStringUTFChars的结果,但直到现在没有错误......

const char *cppFilePaths[elementCount] = {};

for (int i = 0; i < elementCount; i++) {
  jstring jFilePath = (jstring) (env->GetObjectArrayElement(filePaths, i));
  cppFilePaths[i] = env->GetStringUTFChars(jFilePath, 0);
  env->DeleteLocalRef(jFilePath);
}