The main problem is the missing activity object which is given to you via the android_app structure in your main if you make a pure NDK solution. If you only want to make a library which can used by a Java developer, you have no access to the main-activity without given to you by a call. Or, to be more precise, I did not found a way to get the main-activity of a Java App from my c++ code. This leaves me to two options:
1) Use getFilesPath called from java and make it part of a method call of the c++ object (maybe "Init")
2) Give the main-activity to the C++ code (maybe in "init").
I decided for the second one. First I didn't want to give control to the programmer of the Java part where to put what. Second, I got the feeling that I will need the activity for some more reasons.
So I ended at a code snipped from Jong Wook Kim which I modified for my needs:
void here_your_ndk_conform_name_init(JNIEnv* env,jobject mainactivity)
{
jclass cls = env->GetObjectClass(mainactivity);
jmethodID getFilesDir = env->GetMethodID(cls, "getFilesDir", "()Ljava/io/File;");
jobject dirobj = env->CallObjectMethod(mainactivity,getFilesDir);
jclass dir = env->GetObjectClass(dirobj);
jmethodID getStoragePath =
env->GetMethodID(dir, "getAbsolutePath", "()Ljava/lang/String;");
jstring path=(jstring)env->CallObjectMethod(dirobj, getStoragePath);
const char *pathstr=env->GetStringUTFChars(path, 0);
std::string strPath=pathstr;
env->ReleaseStringUTFChars(path, pathstr);
}
where strPath now has the path to your internal storage which you can freely access with iostreams.