利用反射得到android存储路径

获得android手机的存储路径:

 

public String getPrimaryStoragePath(){
        try{
            StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
            Method m = StorageManager.class.getMethod("getVolumePaths", null);
            String[] paths = (String[]) m.invoke(sm, null);
            return paths[0];
        }catch(Exception e){
             Log.e("xxxxxxxxxx", "getPrimaryStoragePath() failed", e);
        }
        return null;
    }
    
public String getSecondaryStoragePath(){
        try{
            StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
            Method m = StorageManager.class.getMethod("getVolumePaths", null);
            String[] paths = (String[]) m.invoke(sm, null);
            return paths[1];
        }catch(Exception e){
             Log.e("xxxxxxxxxx", "getSecondaryStoragePath() failed", e);
        }
        return null;
    }

 

 

 

在StorageManager类中有三个隐藏的方法,用反射得到最后那个方法就可以获得android手机所有可存储路径

/**
     * Gets the state of a volume via its mountpoint.
     * @hide
     */
    public String getVolumeState(String mountPoint) {
        try {
            return mMountService.getVolumeState(mountPoint);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get volume state", e);
            return null;
        }
    }
    /**
     * Returns list of all mountable volumes.
     * @hide
     */
    public StorageVolume[] getVolumeList() {
        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;
        }
    }
    /**
     * Returns list of paths for all mountable volumes.
     * @hide
     */
    public String[] getVolumePaths() {
        StorageVolume[] volumes = getVolumeList();
        if (volumes == null) return null;
        int count = volumes.length;
        String[] paths = new String[count];
        for (int i = 0; i < count; i++) {
            paths[i] = volumes[i].getPath();
        }
        return paths;
    }

 

转载于:https://www.cnblogs.com/wenhui92/p/6242388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值