android_app->activity->internalDataPath still NULL in 2.3.8 NDK r8

前端 未结 1 989
名媛妹妹
名媛妹妹 2021-02-06 12:18

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

I am having issues in ndk-r7b and in ndk-r8 accessing the local read write directories using

1条回答
  •  日久生厌
    2021-02-06 12:47

    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();
    }
    

    0 讨论(0)
提交回复
热议问题