SoFunction
Updated on 2025-03-04

How to get all memory mounted on Android device

The Android system provides the path to obtain memory by the () interface, but the result given by this interface is not what we want. On some devices, it returns internal storage for the mobile phone, and the external storage for the mobile phone that some devices return. Also, some Android devices support expanding multiple SDcards. At this time, if you want to obtain the mount path of all memory, this interface cannot be done.

How to get the location of all memory of Android devices?Or get all mount points

The system provides a StorageManager, which has a method called getVolumeList. The return value of this method is a StorageVolume array. The StorageVolume class encapsulates information such as mount path, mount status, and whether it can be removed. But unfortunately, this method is a hidden API, so we can only call this method through reflection. The following is the source code of this method.

public StorageVolume[] getVolumeList() {
    if (mMountService == null) return new StorageVolume[0];
    try {
      Parcelable[] list = ();
      if (list == null) return new StorageVolume[0];
      int length = ;
      StorageVolume[] result = new StorageVolume[length];
      for (int i = 0; i < length; i++) {
        result[i] = (StorageVolume)list[i];
      }
      return result;
    } catch (RemoteException e) {
      (TAG, "Failed to get volume list", e);
      return null;
    }
  }

Through reflection, all memory of Android device is obtained.

public class StorageInfo {
 public String path;
 public String state;
 public boolean isRemoveable;
 
 public StorageInfo(String path) {
  = path;
 }
 
 public boolean isMounted() {
 return "mounted".equals(state);
 }
}
public static List listAvaliableStorage(Context context) {
    ArrayList storagges = new ArrayList();
    StorageManager storageManager = (StorageManager) (Context.STORAGE_SERVICE);
    try {
      Class<?>[] paramClasses = {};
      Method getVolumeList = ("getVolumeList", paramClasses);
      (true);
      Object[] params = {};
      Object[] invokes = (Object[]) (storageManager, params);
      if (invokes != null) {
        StorageInfo info = null;
        for (int i = 0; i < ; i++) {
          Object obj = invokes[i];
          Method getPath = ().getMethod("getPath", new Class[0]);
          String path = (String) (obj, new Object[0]);
          info = new StorageInfo(path);
          File file = new File();
          if ((()) && (()) && (())) {
            Method isRemovable = ().getMethod("isRemovable", new Class[0]);
            String state = null;
            try {
              Method getVolumeState = ("getVolumeState", );
              state = (String) (storageManager, );
               = state;
            } catch (Exception e) {
              ();
            }
 
            if (()) {
               = ((Boolean) (obj, new Object[0])).booleanValue();
              (info);
            }
          }
        }
      }
    } catch (NoSuchMethodException e1) {
      ();
    } catch (IllegalArgumentException e) {
      ();
    } catch (IllegalAccessException e) {
      ();
    } catch (InvocationTargetException e) {
      ();
    }
    ();
 
    return storagges;
  }

How to tell if the memory is built-in or external storage?

StorageVolume provides an isRemovable() interface, which can be used to call it through reflection to know whether the memory can be removed. Removable memory is considered as an external SDcard, and unremovable memory is considered as a built-in memory.

Method isRemovable = ().getMethod("isRemovable", new Class[0]);

How to determine the mount status of the memory?

As above, the reflection system interface is required to obtain the mount state. Below is a code snippet

Method getVolumeState = ("getVolumeState", );
              state = (String) (storageManager, );
               = state;

Summarize

Through the StorageManager of the reflection system and the interface provided by the StorageVolume class, you can get all memory paths, memory types (built-in storage or external storage), and memory mount status and other information.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.