Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

like image 456
Androider Avatar asked Nov 30 '22 07:11

Androider


1 Answers

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();
}
like image 124
trashkalmar Avatar answered Dec 09 '22 18:12

trashkalmar