android_app->活动 - > internalDataPath还是空的2.3.8 NDK R8internalDataPath、GT、android_app、NDK

2023-09-05 23:51:18 作者:Trust 信任

要给予规范在哪里我测试上,HTC Desire S,安卓2.3.5和NDK-R8。

To give spec on where i tested this, HTC Desire S, Android 2.3.5 and ndk-r8.

我有在NDK-R7B和NDK-R8访问使用android_app->活动 - > internalDataPath或externalDataPath,因为它们都是空的地方读写的目录问题。我看到,这已经张贴在previous版本和更新打算为此,根据这个帖子来解决: -

I am having issues in ndk-r7b and in ndk-r8 accessing the local read write directories using android_app->activity->internalDataPath or externalDataPath as they are both NULL. I see that this has been posted in previous versions and an update was going to fix this according to this post:-

How我写与NativeActivity的内部存储文件系统?

现在或许这被固定在冰淇淋三明治但这远非理想,因为我的工具链支持向后兼容性完全赶上那些谁不更新。所以我的问题是有没有一个已知的固定或我必须手动添加,如/data/data/com.example.mytest/files/somefile.dat的目录结构,直到它固定的吗?

Now perhaps this gets fixed in Ice Cream Sandwich but that's far from ideal as my tool chain supports backwards compatibility perfectly to catch those who don't update. So my question is is there a known fix or do i have to manually add the directory structure like "/data/data/com.example.mytest/files/somefile.dat" untill its fixed ?

推荐答案

从NDK下工作而不使用Java:

The following works from NDK without use of Java:

const char* path = app->activity->internalDataPath;
if (!path) {
    JNIEnv* jni;
    app->activity->vm->AttachCurrentThread(&jni, NULL);

    jclass activityClass = jni->GetObjectClass(app->activity->clazz);
    jmethodID getFilesDir = jni->GetMethodID(activityClass, "getFilesDir", "()Ljava/io/File;");
    jobject fileObject = jni->CallObjectMethod(app->activity->clazz, getFilesDir);
    jclass fileClass = jni->GetObjectClass(fileObject);
    jmethodID getAbsolutePath = jni->GetMethodID(fileClass, "getAbsolutePath", "()Ljava/lang/String;");
    jobject pathObject = jni->CallObjectMethod(fileObject, getAbsolutePath);
    path = jni->GetStringUTFChars((jstring)pathObject, NULL);

    jni->DeleteLocalRef(pathObject);
    jni->DeleteLocalRef(fileClass);
    jni->DeleteLocalRef(fileObject);
    jni->DeleteLocalRef(activityClass);

    app->activity->vm->DetachCurrentThread();
}