android7.0 SD Card 路径获取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiashaohua/article/details/72823722

现在网上关于外置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


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页