下面是Ertebolle引用的GetMethodID / CallObjectMethod过程。如果您使用的是纯本地应用程序(例如Visual Studio 2015构建),并且不能使用Java代码,则这是必要的。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;
}