问题
I'm working on porting PC OpenGL application onto Android. I've chosen to use that NDK android_native_app_glue framework stuff. As I understood, it would allow to me stay on C++ and write no even single JAVA code line. And it sounded promising.
The first unclear thing to me it data saving/loading. My application's data format is binary and my original PC-related code uses plain "stdio.h" FILE operations: fopen, fread, fwrite, etc to create, write and read from "mygame.bin" file. How do I port it onto Android?
Having googled it, I found that I must store and then use a "java environment" variable:
JNIEnv *g_jniEnv = 0;
void android_main(struct android_app* state) {
g_jniEnv = state->activity->env;
}
However, I still do not realize how to use this g_jniEnv to perform file operations.
Update: Okay, I found that in Java, data saving can be performed as follows:
String string = "hello world!";
FileOutputStream fos = openFileOutput("mygame.bin", Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
So my questions is: given a pointer to JNIEnv, how do I rewrite this code fragment in C++?
回答1:
You can still use stdio.h in an NDK project. #include and pound away.
The only trick is getting the path to your application's private data folder. It's available as state->activity->internalDataPath
in android_main
. Outside of that path, the app has no filesystem rights.
You will probably need your JNIEnv
eventually one way or another. The entirety of Java API is not mirrored for the native subsystem; so hold on to that env
. But native file I/O is right there.
来源:https://stackoverflow.com/questions/12532259/android-ndk-data-save-load