Android获取SDCard路径/Android获取存储器挂载点

获取Android所有SDCard(存储器)路径

已知获取的方法有二

我们来探究下这些方法,我们的目的就是得到能准确实现功能的方法,(务必提高兼容性,考虑Android版本的差异和手机型号的差异)


1、反射

  • 别人的总结—1(完美实现需求)

    • 我的实现
    /**
    杨铭 Created by Tom on 2016/7/30. <p>Email:771365380@qq.com</p> <p>Mobile phone:15133350726</p>
    <p/>
    获取手机挂载点的各种形式
    <p/>
    注意:适用Android版本为[sdk14:sdk23]
    <p/>
    自api16一下的版本在StorageVolume方法中没有getPathFile
    */
    public class GetMountPoint
    {
         
    private Context context;
    private final static String tag = "GetMountPoint";
    
    /** 构造方法 */
    private GetMountPoint(Context context)
    {
        this.context = context;
    }
    
    /** 之所以用这种方法初始化时为了防止使用的时候没有检查SDK版本导致出错 */
    public static GetMountPoint GetMountPointInstance(Context context)
    {
        if (14 <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT <= 23)
        {
            return new GetMountPoint(context);
        }
        else
        {
            Log.e(tag, "本类不支持当前SDK版本");
            return null;
        }
    }
    
    /** 核心操作-获取所有挂载点信息。 */
    public List<MountPoint> getMountPoint()
    {
        try
        {
            Class class_StorageManager = StorageManager.class;
            Method method_getVolumeList = class_StorageManager.getMethod("getVolumeList");
            Method method_getVolumeState = class_StorageManager
                    .getMethod("getVolumeState", String.class);
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            Class class_StorageVolume = Class.forName("android.os.storage.StorageVolume");
            Method method_isRemovable = class_StorageVolume.getMethod("isRemovable");
            Method method_getPath = class_StorageVolume.getMethod("getPath");
            Method method_getPathFile = null;
            if (Build.VERSION.SDK_INT >= 17)
            {
        // 自api16一下的版本在StorageVolume方法中没有getPathFile
                method_getPathFile = class_StorageVolume.getMethod("getPathFile");
            }
            Object[] objArray = (Object[]) method_getVolumeList.invoke(sm);
    
            //region 所有挂载点File---附带是内置存储卡还是外置存储卡的标志
            List<MountPoint> result = new ArrayList<>();
            for (Object value : objArray)
            {
                String path = (String) method_getPath.invoke(value);
                File file;
                if (Build.VERSION.SDK_INT >= 17)
                {
                    file = (File) method_getPathFile.invoke(value);
                }
                else
                {
                    file = new File(path);
                }
    
                boolean isRemovable = (boolean) method_isRemovable.invoke(value);
    
                boolean isMounted;
                String getVolumeState = (String) method_getVolumeState.invoke(sm, path);//获取挂载状态。
                if (getVolumeState.equals(Environment.MEDIA_MOUNTED))
                {
                    isMounted = true;
                }
                else
                {
                    isMounted = false;
                }
    
                result.add(new MountPoint(file, isRemovable, isMounted));
            }
            return result;
            //endregion
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    /** 获取处于挂载状态的挂载点的信息 */
    public List<MountPoint> getMountedPoint()
    {
        List<MountPoint> result = this.getMountPoint();
        for (MountPoint value : result)
        {
            if (!value.isMounted)
            {
                result.remove(value);
            }
        }
        return result;
    }
    
    public class MountPoint
    {
         
        private File file;
        /** 用于判断是否为内置存储卡,如果为true就是代表本挂载点可以移除,就是外置存储卡,否则反之 */
        private boolean isRemovable;
        /** 用于标示,这段代码执行的时候这个出处卡是否处于挂载状态,如果是为true,否则反之 */
        private boolean isMounted;
    
        public MountPoint(File file, boolean isRemovable, boolean isMounted)
        {
            this.file = file;
            this.isMounted = isMounted;
            this.isRemovable = isR
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值