如下所示:
jclass envcls = env->FindClass("android/os/Environment"); //获得类引用 if (envcls == nullptr) return 0; //找到对应的类,该类是静态的返回值是File jmethodID id = env->GetStaticMethodID(envcls, "getExternalStorageDirectory", "()Ljava/io/File;"); //调用上述id获得的方法,返回对象即File file=Enviroment.getExternalStorageDirectory() //其实就是通过Enviroment调用 getExternalStorageDirectory() jobject fileObj = env->CallStaticObjectMethod(envcls,id,""); //通过上述方法返回的对象创建一个引用即File对象 jclass flieClass = env->GetObjectClass(fileObj); //或得类引用 //在调用File对象的getPath()方法获取该方法的ID,返回值为String 参数为空 jmethodID getpathId = env->GetMethodID(flieClass, "getPath", "()Ljava/lang/String;"); //调用该方法及最终获得存储卡的根目录 jstring pathStr = (jstring)env->CallObjectMethod(fileObj,getpathId,""); path = env->GetStringUTFChars(pathStr,NULL);