这是完全可能的,但并非没有一些 Java 代码。
编辑:提供以下内容作为数据库的替代品。能够从本机代码向文件读取和写入持久数据将比数据库灵活得多......
假设您想从驻留在文件系统上的文件(二进制或纯文本)中存储和检索一些数据,这些将是要采取的步骤:
JAVA:获取应用的存储位置并检查它是否可用于读写
JAVA : 如果上面是肯定的,通过JNI传给native层
NATIVE :使用存储参数来读/写你的文件
好的,到目前为止的摘要;让我们来看看代码:
1A) 检索和检查存储:
private boolean checkExternalStorageState(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
android.util.Log.i("YourActivity", "External storage is available for read/write...", null);
return true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media : NOT ok
android.util.Log.e("YourActivity", "External storage is read only...", null);
return false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
android.util.Log.e("YourActivity", "External storage is not mounted or read only...", null);
return false;
}
}
获取存储位置:
private get storageLocation(){
File externalAppDir = getExternalFilesDir(null);
String storagePath = externalAppDir.getAbsolutePath()
}
1B)您可能还想检查文件是否存在(您也可以在本机部分执行此操作)
private boolean fileExists(String file) {
String filePath = storagePath + "/" + file;
// see if our file exists
File dataFile = new File(filePath);
if(dataFile.exists() && dataFile.isFile())
{
// file exists
return true;
}
else
{
// file does not exist
return false;
}
}
2)将其传递给本机层:
JAVA部分:
// Wrapper for native library
public class YourNativeLib {
static {
// load required libs here
System.loadLibrary("yournativelib");
}
public static native long initGlobalStorage(String storagePath);
...enter more functions here
}
原生部分:
JNIEXPORT jlong JNICALL Java_com_whatever_YourNativeLib_initGlobalStorage(JNIEnv *env, jobject obj, jstring storagePath)
{
jlong data = 0;
// convert strings
const char *myStoragePath = env->GetStringUTFChars(storagepath, 0);
// and now you can use "myStoragePath" to read/write files in c/c++
//release strings
env->ReleaseStringUTFChars(storagePath, myStoragePath);
return data;
}
如何在 c/c++ 中读取/写入二进制文件或文本文件已有详细记录,我将由您决定。