Jni: Reading A Text File In C Code And Returning To Sdk
Im trying to create an android app to read text from a text file using NDK .My C code reads a string into a variable and returns the string variable to java code .But when I run th
Solution 1:
Try this:
JNIEXPORT jstring JNICALL Java_com_example_openfile_MainActivity_ndkopenfile
(JNIEnv *env, jobject this)
{
char myStr[20];
FILE* fp = fopen("/sdcard/x.txt","w+");
if(fp!=NULL)
{
fgets(myStr,20,fp);
fflush(fp);
fclose(fp);
return(*env)->NewStringUTF(env,myStr);
}
else
{
fclose(fp);
return(*env)->NewStringUTF(env,"Error opening file!");
}
}
This technique works perfectly !!! Tested.
Post a Comment for "Jni: Reading A Text File In C Code And Returning To Sdk"