Creating temporary files in Android with NDK

前端 未结 5 1428
一生所求
一生所求 2020-12-03 07:10

I am currently working on a C-based, NDK-based, Android application. This application needs to create temporary files. On a regular Linux system, I would use tmpfile

5条回答
  •  抹茶落季
    2020-12-03 07:54

    Below is the GetMethodID / CallObjectMethod procedure that Ertebolle refers to. It is necessary if you are working with a pure native app (such as built by Visual Studio 2015) and cannot use java code.

    std::string android_temp_folder( struct android_app *app ) {
        JNIEnv* env;
        app->activity->vm->AttachCurrentThread( &env, NULL );
    
        jclass activityClass = env->FindClass( "android/app/NativeActivity" );
        jmethodID getCacheDir = env->GetMethodID( activityClass, "getCacheDir", "()Ljava/io/File;" );
        jobject cache_dir = env->CallObjectMethod( app->activity->clazz, getCacheDir );
    
        jclass fileClass = env->FindClass( "java/io/File" );
        jmethodID getPath = env->GetMethodID( fileClass, "getPath", "()Ljava/lang/String;" );
        jstring path_string = (jstring)env->CallObjectMethod( cache_dir, getPath );
    
        const char *path_chars = env->GetStringUTFChars( path_string, NULL );
        std::string temp_folder( path_chars );
    
        env->ReleaseStringUTFChars( path_string, path_chars );
        app->activity->vm->DetachCurrentThread();
        return temp_folder;
    }
    

提交回复
热议问题