获得手机sdCard

http://www.2cto.com/kf/201502/377175.html


http://www.2cto.com/kf/201502/377175.html

http://blog.csdn.net/chaoyue0071/article/details/47045629

http://www.android100.org/html/201502/14/118622.html

http://bbs.csdn.net/topics/391891314

Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方法返回的就是外置sd卡的根路径。

但随着Android进入4.x时代,大部分Android设备都已经内置一个较大存储卡,甚至不提供外置存储卡插槽了,此时Environment.getExternalStorageDirectory()返回的路径其实是由内置存储卡虚拟出来的一个目录,用户在这一目录下有全部权限。

按理说Android应该继续提供其他的方法来返回其他的外置存储卡的路径,但这一次Android却没有,因为Android系统开发者们发现之前直接允许程序任意访问sd卡的做法导致sd卡上垃圾文件的不可管理性,尤其在程序被卸载后,由于不知道那些文件是由这一程序创建的,所以无法关联删除,因此Android 4.0并不给程序开发者提供外置存储卡的路径,而是提供了诸如getExternalFilesDirs()和getExternalCacheDirs()这样的方法,直接给开发者提供一个限制内的存储文件的空间,这个空间是与程序关联的,因此在卸载程序时这两个目录下的文件也会被关联删除。

闲话到此,主要是为了说明Android 4.0之后,系统不希望开发者直接访问sd卡,所以也没有提供查询多sd卡路径的方法(在2.x和3.x时代末期我们都以为4.0会提供这样的查询接口)。

但是,Android系统不提供并不意味着就没有办法了,Android说到底还是Linux系统,因此外置sd卡必然是按照Linux的挂载方式挂载到系统中的。因此目前比较主流的方法就是绕过Android直接去读取Linux底层的挂载文件来找到系统中挂载的外置存储器。

参考代码:(这个代码是从网上找的,跟我之前写的思路差不多,参考这个改改吧,其实不用这么复杂,返回一个Set或者List把所有路径都返回就好了)

Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
     public  static  Map<String, File> getAllStorageLocations() {
         Map<String, File> map =  new  HashMap<String, File>( 10 );
 
         List<String> mMounts =  new  ArrayList<String>( 10 );
         List<String> mVold =  new  ArrayList<String>( 10 );
         mMounts.add( "/mnt/sdcard" );
         mVold.add( "/mnt/sdcard" );
 
         try  {
             File mountFile =  new  File( "/proc/mounts" );
             if  (mountFile.exists()) {
                 Scanner scanner =  new  Scanner(mountFile);
                 while  (scanner.hasNext()) {
                     String line = scanner.nextLine();
                     if  (line.startsWith( "/dev/block/vold/" )) {
                         String[] lineElements = line.split( " " );
                         String element = lineElements[ 1 ];
 
                         // don't add the default mount path
                         // it's already in the list.
                         if  (!element.equals( "/mnt/sdcard" ))
                             mMounts.add(element);
                     }
                 }
             }
         catch  (Exception e) {
             e.printStackTrace();
         }
 
         try  {
             File voldFile =  new  File( "/system/etc/vold.fstab" );
             if  (voldFile.exists()) {
                 Scanner scanner =  new  Scanner(voldFile);
                 while  (scanner.hasNext()) {
                     String line = scanner.nextLine();
                     if  (line.startsWith( "dev_mount" )) {
                         String[] lineElements = line.split( " " );
                         String element = lineElements[ 2 ];
 
                         if  (element.contains( ":" ))
                             element = element.substring( 0 , element.indexOf( ":" ));
                         if  (!element.equals( "/mnt/sdcard" ))
                             mVold.add(element);
                     }
                 }
             }
         catch  (Exception e) {
             e.printStackTrace();
         }
 
 
         for  ( int  i =  0 ; i < mMounts.size(); i++) {
             String mount = mMounts.get(i);
             if  (!mVold.contains(mount))
                 mMounts.remove(i--);
         }
         mVold.clear();
 
         List<String> mountHash =  new  ArrayList<String>( 10 );
 
         for  (String mount : mMounts) {
             File root =  new  File(mount);
             if  (root.exists() && root.isDirectory() && root.canWrite()) {
                 File[] list = root.listFiles();
                 String hash =  "[" ;
                 if  (list !=  null ) {
                     for  (File f : list) {
                         hash += f.getName().hashCode() +  ":"  + f.length() +  ", " ;
                     }
                 }
                 hash +=  "]" ;
                 if  (!mountHash.contains(hash)) {
                     String key = SD_CARD +  "_"  + map.size();
                     if  (map.size() ==  0 ) {
                         key = SD_CARD;
                     else  if  (map.size() ==  1 ) {
                         key = EXTERNAL_SD_CARD;
                     }
                     mountHash.add(hash);
                     map.put(key, root);
                 }
             }
         }
         mMounts.clear();
         if  (map.isEmpty()) {
             map.put(SD_CARD, Environment.getExternalStorageDirectory());
         }
         return  map;
     }


你可能感兴趣的:(获得手机sdCard)