Androider
Androider

Reputation: 21345

Syntax for converting jstring and appending

JNIEXPORT int JNICALL Java_com_ndkfoo_test_GL2JNILib_step2(JNIEnv * env, jobject obj,  jstring filePath)
{
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, filePath, NULL);
    char* fullPath=str.append("FileName.txt"); // error
    char* fullPath2=str+"fileName.txt"          // error
}

Could someone please indicate the right syntax to create define fullPathName? I think passing jstring in is correct but I don't know how to convert to pull path name for fopen().

Upvotes: 4

Views: 11086

Answers (1)

trashkalmar
trashkalmar

Reputation: 883

Try using this function which converts jstring to std:string:

void GetJStringContent(JNIEnv *AEnv, jstring AStr, std::string &ARes) {
  if (!AStr) {
    ARes.clear();
    return;
  }

  const char *s = AEnv->GetStringUTFChars(AStr,NULL);
  ARes=s;
  AEnv->ReleaseStringUTFChars(AStr,s);
}

Solution of your task:

JNIEXPORT int JNICALL Java_com_ndkfoo_test_GL2JNILib_step2(JNIEnv * env, jobject obj,  jstring filePath) {
    std::string str;
    GetJStringContent(env,filePath,str);
    const char *fullPath = str.append("FileName.txt").c_str();
}

Upvotes: 20

Related Questions