2011-09-27 174 views
2

我有Java Android应用程序(TestApp)。从我TestApp我打电话的功能从JNI代码:如何在jni中指定当前工作路径

JNIEXPORT jint JNICALL Java_com_app_test_testApp_CreateFile(JNIEnv* env, jobject thiz, jobject jclass) { 
    pFile = fopen ("/NDK_Log.txt", "a+"); 
    // Get's current date time and print it in the log file. 
    dateTime = time(NULL); 
    currentTime = ctime(&dateTime); 
    fprintf(pFile, "\n\n--------------------------------------------------------------------------\n"); 
    fprintf(pFile, "\t\t\t\t\t\t %s", currentTime); 
    fprintf(pFile, "--------------------------------------------------------------------------\n\n"); 
    fprintf(pFile, ">>> Enter Initialize <<<\n"); 
    #endif 

    return 0; 
} 

我要创建文件“数据/数据/ com.app.test.testApp /”文件夹,但我不能,我在做什么错,以及如何指定当前工作目录或给出当前应用程序的路径?

回答

1

你不能依靠fopen来使用“当前工作目录”。

在设备内存中,只能访问应用程序数据文件夹中的文件。 你可以让你的应用程序的私有文件夹是这样的:

String dir = getPackageManager().getPackageInfo("com.example.app", 0).applicationInfo.dataDir; 

这将是/数据/数据文件夹的地方。

+0

这意味着我无法通过NDK获取目录? –

+0

您可以使用对Java代码的JNI调用来获取目录。 –

0

Android提供了getcwd(),但我不认为这就是你真正需要的。

如前所述,您需要从Java中检索基本路径部分。如果需要,您可以在Java中实现一个方法来获取路径并通过JNI通过JNI调用它。

相关问题