Android ndk Method for Obtaining the Root Directory of Memory Card in Mobile Phone

  • 2021-09-24 23:38:58
  • OfStack

As shown below:


 jclass envcls = env->FindClass("android/os/Environment"); // Get a class reference 
 if (envcls == nullptr) return 0;
 
 // Find the corresponding class, which is static and the return value is File
 jmethodID id = env->GetStaticMethodID(envcls, "getExternalStorageDirectory", "()Ljava/io/File;");
 
 // Call the above id The return object is the method obtained by the File file=Enviroment.getExternalStorageDirectory()
 // In fact, it is through Enviroment Call  getExternalStorageDirectory()
 jobject fileObj = env->CallStaticObjectMethod(envcls,id,"");
 
 // The object returned by the above method is created 1 A reference is File Object 
 jclass flieClass = env->GetObjectClass(fileObj); // Or class reference 
 // When calling File Object's getPath() Method to get the ID And the return value is String  Parameter is empty 
 jmethodID getpathId = env->GetMethodID(flieClass, "getPath", "()Ljava/lang/String;");
 // Call this method and finally get the root directory of the memory card 
 jstring pathStr = (jstring)env->CallObjectMethod(fileObj,getpathId,"");
 
 path = env->GetStringUTFChars(pathStr,NULL);

Related articles: