Mobile phone storage and SD card of all storage devices mounted to Android devices

Source: Internet
Author: User

Mobile phone storage and SD card of all storage devices mounted to Android devices
Obtain all memory attached to an Android device

The android system provides Environment. the getExternalStorageDirectory () interface obtains the path of the storage. However, this interface does not always give us the desired result. On some devices, it returns the internal storage of the mobile phone, the external storage of the mobile phone returned by some devices. In addition, some Android devices support the expansion of multiple sdcards. In this case, this interface cannot be used to obtain the Mount paths of all storage devices.

  • How can I obtain the location of all the memory of the Android device? In other words, the system provides a StorageManager for obtaining all mount points. The getVolumeList method returns a StorageVolume array. The StorageVolume class encapsulates the Mount path and 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. below is the method.

     

    Only the mobile phone storage directory is returned.
    Public File getStorage () {StorageManager manager = (StorageManager) getSystemService (STORAGE_SERVICE); try {Class
       [] ParamClasses ={}; Method getVolumeList; getVolumeList = StorageManager. class. getMethod ("getVolumeList", paramClasses); getVolumeList. setAccessible (true); Object [] params = {}; Object [] invokes = (Object []) getVolumeList. invoke (manager, params); if (invokes! = Null) {for (int I = 0; I <invokes. length; I ++) {Object obj = invokes [I]; Method getPath = obj. getClass (). getMethod ("getPath", new Class [0]); String path = (String) getPath. invoke (obj, new Object [0]); File file = new File (path); if (file. exists () & (file. isDirectory () & (file. canWrite () {Method isRemovable = obj. getClass (). getMethod ("isRemovable", new Class [0]); String state = null; try {Method getVolumeState = StorageManager. class. getMethod ("getVolumeState", String. class); state = (String) getVolumeState. invoke (manager, path);} catch (Exception e) {e. printStackTrace ();} boolean canRemovable = (Boolean) isRemovable. invoke (obj, new Object [0]). booleanValue (); // It cannot be deleted and is mounted to the mobile phone storage if (! CanRemovable & state. equals (Environment. MEDIA_MOUNTED) {return file ;}}}} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null ;}



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

     

  • Obtain all the memory of the Android device through reflection.

  •  

    1 2 3 4 5 6 7 8 9 10 11 12 13 PublicclassStorageInfo {publicStringpath; publicStringstate; publish; publicStorageInfo (Stringpath) {this. path = path;} publicbooleanisMounted () {return "mounted". equals (state );}}

     

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



     
  •  
  • How can we determine whether the storage is internal storage or external storage? The StorageVolume class provides an isRemovable () interface, which can be called through reflection to determine whether the memory can be removed. Identifies the memory that can be removed as an external sdcard, and the memory that cannot be removed as a built-in memory.

     

    1 MethodisRemovable = obj. getClass (). getMethod ("isRemovable", newClass [0]);

     

     

  • How can I determine the storage mounting status? The same as above, the system interface needs to be reflected to obtain the Mount status. The following is a code snippet.

     

    1 2 3 MethodgetVolumeState = StorageManager. class. getMethod ("getVolumeState", String. class); state = (String) getVolumeState. invoke (storageManager, info. path); info. state = state;

     

  • Summary: Through the interfaces provided by the StorageManager and StorageVolume classes of the reflection system, you can get all the storage paths mounted to the Android device and the storage types (internal storage or external storage ), information such as the storage mounting status.
  •  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.