SoFunction
Updated on 2025-04-05

Android N method to get external SD card or mount USB disk path

There is no direct method to obtain the external SD card or mount the USB flash drive on Android N. You can obtain the built-in SD card path through the following method

().getAbsolutePath();

By checking the getExternalStorageDirectory source code, it is found that Android just does not have an open interface to obtain it.

 public static File getExternalStorageDirectory() {
  throwIfUserRequired();
  return ()[0];
 }

The first value in () that is retrieved by the built-in SD card, by checking the public methods of StorageManager, and posting StorageManager@getStorageVolumes, you can also get all StorageVolumes, but you can only call some simple methods through the StorageVolume object. I found that StorageVolume has many hidden methods as follows:

frameworks/base/core/java/android/os/storage/
 /**
  * Returns true if the volume is removable.
  *
  * @return is removable
  */
 public boolean isRemovable() {
  return mRemovable;
 }
 
 /**
  * Returns the mount path for the volume.
  *
  * @return the mount path
  * @hide
  */
 public String getPath() {
  return ();
 }
 
 /** {@hide} */
 public File getPathFile() {
  return mPath;
 }

There is no public interface to call these methods, so you can only think of reflection. The specific implementation method is as follows:

1. Add required permissions to clear the file

 <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS"/>
 <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name=".READ_EXTERNAL_STORAGE"/>

2. Obtain the external SD card or mount the USB flash drive through reflection

  private StorageManager mStorageManager;
  mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
  //Get all mounted devices (internal SD card, external SD card, mounted USB disk)  List&lt;StorageVolume&gt; volumes = ();
  try {
   Class&lt;?&gt; storageVolumeClazz = Class
     .forName("");
   //Calling system hide method through reflection   Method getPath = ("getPath");
   Method isRemovable = ("isRemovable");
   for (int i = 0; i &lt; (); i++) {
    StorageVolume storageVolume = (i);//Get the StorageVolume for each mounted    
    //Call getPath and isRemovable through reflection    String storagePath = (String) (storageVolume); //Get the path    boolean isRemovableResult = (boolean) (storageVolume);//Is it removable    String description = (this);
    ("jason", " i=" + i + " ,storagePath=" + storagePath
      + " ,isRemovableResult=" + isRemovableResult +" ,description="+description);
   }
  } catch (Exception e) {
   ("jason", " e:" + e);
  }

The above method of obtaining an external SD card or mounting a USB flash drive for Android N is all the content I share with you. I hope you can give you a reference and I hope you can support me more.