获取内置/外置sd卡路径

获取内置/外置sd卡路径
Software #112152

获取内外置T卡位置:StorageManager.java类
http://192.168.3.77:8989/wcp/webdoc/view/Pub402880cd5c5da6e7015dab1bb5b40ed2.html 达哥的WCP+廖文星的bug 112152
可参考http://blog.csdn.net/rgen_xiao/article/details/55506025
没有外置T卡
01-04 20:19:12.956 3408-3408/com.example.sharedpreferencestest D/ContentValues: 
onCreate: getStoragePath(this,true)==null 

内置T卡位置
01-04 20:19:12.959 3408-3408/com.example.sharedpreferencestest D/ContentValues: 
onCreate: getStoragePath(this,false)==/storage/emulated/0 

有外置T卡
01-04 20:21:05.160 4018-4018/com.example.sharedpreferencestest D/ContentValues: 
onCreate: getStoragePath(this,true)==/storage/B403-1BF6 

内置T卡位置
01-04 20:21:05.164 4018-4018/com.example.sharedpreferencestest D/ContentValues: 
onCreate: getStoragePath(this,false)==/storage/emulated/0

scard0:指系统内部存储
scard1:指外插的sd卡

方法一:
    //is_removable false获取内置sd路径  true获取外置sd路径  
    private String getStoragePath(Context mContext,boolean is_removable){
        StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
        Class<?> storageVolumeClazz = null;
        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");//获取StorageVolume类
            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");//通过反射获取StorageManager中方法
            Method getPath = storageVolumeClazz.getMethod("getPath");//通过反射获取StorageVolume中方法
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");//通过反射获取StorageVolume中方法
            Object result = getVolumeList.invoke(mStorageManager);//通过Method的invoke方法获取当前存在的可以获取到的存储列表
            final int length = Array.getLength(result);//存储列表的数目
            //String pathappend="";
            //下面的for循环会得到当前所有的存储路径:包括内置、外置、otg,但是我们平时的otg不常用,在客户有需求时候,可以稍作修改
            for(int i=0;i<length;i++){
                Object storageVolumeElement = Array.get(result,i);//获取存储元素
                String path = (String)getPath.invoke(storageVolumeElement);//通过存储元素获取对应路径
                boolean removable = (Boolean)isRemovable.invoke(storageVolumeElement);//通过存储元素获取是否支持可拆卸
                if(is_removable == removable){
                    return path;
                }
                //pathappend=pathappend+",,,"+path;
            }
            return null;
            //对N平台的获取到的元素顺序是:内置,,,外置,,,otg:,,,/storage/emulated/0,,,/storage/70FF-15E5,,,/storage/7AB3-AE0B
            //return pathappend;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }
    
方法一缩略写法:
    public void getPrimaryStoragePath() {
        try {
            StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
            String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
            // first element in paths[] is primary storage path
            Log.d(TAG, "getPrimaryStoragePath: getStoragePath(this,true)=="+paths[0]);//内置sd路径
            Log.d(TAG, "getPrimaryStoragePath: getStoragePath(this,true)=="+paths[1]);//外置sd路径
        } catch (Exception e) {
            Log.e(TAG, "getPrimaryStoragePath() failed", e);
        }
    }
    
廖文星:(此方法可在源码里写,在apk里写sm.getVolumePaths()出错!)
    public void getPrimaryStoragePath() {
        //Redmine#112152 liaowenxing modified for changing record path 2017.12.27,begin
        StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        String[] paths = sm.getVolumePaths();
        if (paths.length > 1)
            sampleDir = new File(paths[1]);
        sampleDir = new File(sampleDir.getAbsolutePath() + "/Recorder/Call Recorder");
        //Redmine#112152 liaowenxing modified for changing record path 2017.12.27,end
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值