开发者

How to specify current working path in jni

开发者 https://www.devze.com 2023-04-08 12:03 出处:网络
I have Java Android application (TestApp).From my TestApp I call function from jni code: JNIEXPORT jint JN开发者_C百科ICALL Java_com_app_test_testApp_CreateFile( JNIEnv* env, jobject thiz, jobject jc

I have Java Android application (TestApp). From my TestApp I call function from jni code:

JNIEXPORT jint JN开发者_C百科ICALL 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;
}

I want to create file in "data/data/com.app.test.testApp/" folder but I can't, what I am doing wrong, and how I can specify current working directory or give current path of application ?


You can't rely on fopen to use "current working directory".

In device internal memory, you can access only files in your app's data folder. And you may get your app's private folder this way:

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

It will be somewhere in /data/data folder.


Android provides getcwd(), but I don't think that's what you really need.

As already mentioned, you'll want to retrieve the base path portion from Java. You can implement a method in Java to get the path and call it from C/C++ via JNI if you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号