Android的NDK数据保存/载入 [英] android ndk data save/load
问题描述
我工作的移植PC OpenGL应用程序的Android上。我选择使用NDK android_native_app_glue框架的东西。我的理解,这将允许我留在C ++写,甚至没有一个Java code线。它听起来前途。
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.
第一件事不清楚对我来说,数据的保存/载入。我的应用程序的数据格式是二进制和我原来的PC相关的code使用普通的stdio.h中文件操作:FOPEN,FREAD,FWRITE等创建,写入和mygame.bin文件中读取。我该如何将它移植到Android的?
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?
已经GOOGLE了它,我发现我必须存储,然后用一个Java环境变量:
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;
}
不过,我仍然不知道如何使用此g_jniEnv进行文件操作。
However, I still do not realize how to use this g_jniEnv to perform file operations.
更新:
好吧,我发现,在Java中,数据保存可以如下进行:
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();
所以我的问题是:给出一个指向JNIEnv的,我怎么用C重写这个code ++片段
So my questions is: given a pointer to JNIEnv, how do I rewrite this code fragment in C++?
推荐答案
您仍然可以使用文件stdio.h在NDK项目。 #包括和英镑走。
You can still use stdio.h in an NDK project. #include and pound away.
唯一的技巧越来越路径到您的应用程序的私有数据文件夹。它可以作为语句>活动 - GT&; internalDataPath
在 android_main
。这条道路外,该应用程序有没有文件系统权限。
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.
您可能会需要你的的JNIEnv
最终这种或那种方式。的Java API的全部未镜像为天然子系统;因此要坚持到 ENV
。不过,本地文件I / O是在那里。
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.
这篇关于Android的NDK数据保存/载入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!