android7.0 SD Card 路径获取

现在网上关于外置SD Card路径的文章大多已经过时,现将相关接口/方法的使用和得到的实际路径贴在下面,供大家参考之用,尤其要注意getExternalStorageDirectory不再是外置SD卡的路径了,而是一个内置的虚拟SD卡,并且对于多用户来讲,后面带一个数字来区分用户路径,主用户id为0,相关说明见链接文档,另附一篇SD  mount的文章链接,虽然也算过时了,但框架和分析问题的思路还在。


log:

05-31 09:54:00.009 6445-6445/? D/MainActivity: getSdDirectory2  mStorageManager=android.os.storage.StorageManager@58e92c6
05-31 09:54:00.010 6445-6445/? D/MainActivity: getSdDirectory2  getVolumeList=public android.os.storage.StorageVolume[] android.os.storage.StorageManager.getVolumeList()
05-31 09:54:00.015 6445-6445/? D/MainActivity: getSdDirectory2  length=2
05-31 09:54:00.015 6445-6445/? D/MainActivity: getSdDirectory2 path=/storage/emulated/10  removable=false getState=mounted
05-31 09:54:00.015 6445-6445/? D/MainActivity: getSdDirectory2 path=/storage/56A1-9404  removable=true getState=mounted
05-31 09:54:00.028 6445-6445/? D/MainActivity: getExternalStorageDirectory =/storage/emulated/10
05-31 09:54:00.029 6445-6445/? D/MainActivity: getRootDirectory =/system



code:

 

             StorageVolume[] volumes = null;

             Object invokeVolumeList = null;

             Method getVolumeList= null;

               

              mStorageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);

              try {

 

           Log.d(TAG, "getSdDirectory2  mStorageManager="+mStorageManager);


                getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
                Log.d(TAG, "getSdDirectory2  getVolumeList="+getVolumeList);

 

                  try {

                   final Class<?>storageValumeClazz = Class.forName("android.os.storage.StorageVolume");

 

                   final MethodgetPath= storageValumeClazz.getMethod("getPath");

                   Method isRemovable = storageValumeClazz.getMethod("isRemovable");

                   Method mGetState = null;

 

                   if (Build.VERSION.SDK_INT >Build.VERSION_CODES.KITKAT) {

                      try {

                           mGetState = storageValumeClazz.getMethod("getState");

                       } catch(NoSuchMethodException e) {

                           e.printStackTrace();

                       }

                   }

 

 

                      try {

                               

                          invokeVolumeList= getVolumeList.invoke(mStorageManager);

                            

                      }catch (Exception e) {

                            Log.e(TAG,"couldn't talkto volumes", e);

                       }

                  

                       final int length = Array.getLength(invokeVolumeList);

                      Log.d(TAG,"getSdDirectory2 length="+length);

                      ArrayList<StorageBean> list = newArrayList<StorageBean>();

 

                     

                      for(int i = 0; i<length ;i++) {

                         

                          final Object storageValume= Array.get(invokeVolumeList, i);//

                         final String path =(String) getPath.invoke(storageValume);

                         final boolean removable =(Boolean) isRemovable.invoke(storageValume);

                         String state = null;

                         if (mGetState !=null) {

                             state = (String) mGetState.invoke(storageValume);

                         }

                        Log.d(TAG,"getSdDirectory2path="+path+"  removable="+removable+" getState="+state);

           

                      }

                  } catch (Exception e) {

                     Log.e(TAG,"couldn't talkto MountService", e);

                  }

         

              } catch(NoSuchMethodException e) {

                 e.printStackTrace();

 

              }

 

参考文章:


Android6.0如何判断有无外置SD卡(TF卡),并读写

http://m.blog.csdn.net/article/details?id=52671938


挂载流程:

http://blog.csdn.net/fe421504975/article/details/8240190


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值