android ndk data save/load

醉酒当歌 提交于 2019-12-07 18:14:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!