reverse_xiaoyu

忘记并不可怕,可怕的是你从来就都不知道!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、内存、内部存储、外部存储

概述

  之前公司项目,涉及到安卓获取USB路径的问题,这个问题,网上的方法千奇百怪,结合自己的项目经历,糅合了一些博文的知识,整合了自己的见解。

  打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的时候又是清除的哪里的数据?

  在android开发中我们常常听到这样几个概念,内存,内部存储,外部存储,很多人常常将这三个东西搞混,那么我们今天就先来详细说说这三个东西是怎么回事?先来看张图。

内存我们在英文中称作memory,内部存储,我们称为InternalStorage,外部存储我们称为ExternalStorage,这在英文中本不会产生歧义,但是当我们翻译为中文之后,前两个都简称为内存,于是,混了。那么究竟什么是内部存储什么是外部存储呢?

 

 这里有三个文件夹需要我们重视,一个是data,一个是mnt,一个是storage,我们下面就详细说说这三个文件夹。

1.内部存储

  data文件夹就是我们常说的内部存储,当我们打开data文件夹之后(没有root的手机不能打开该文件夹),里边有两个文件夹值得我们关注(我这里以公司的安卓样板的目录举例,不泄露任何目标,仅仅用来展示安卓目录!!!!!!!毕竟样板比较容易获取root权限,毕竟我们自己就是一手厂家,拿手机的话去root我就太麻烦了,而且会造成不可预知的损失)如下:

 可以看到,我们进入data目录下后,再执行其他指令的,会显示permission denied,权限禁止,样板获取root权限,只需要 su就行啦,跟linux指令是一样的。

图片仅仅演示过程,大家只需要看标记红框的目录就行,这些目录不管是什么安卓手机,平板,安卓产品是一致的,大家只需要关注红框目录就行

获取到root权限之后,就可以看到下面两个目录了,这事着重要在意的两个目录

 一个文件夹是app文件夹,还有一个文件夹就是data文件夹,app文件夹里存放着我们所有安装的app的apk文件,其实,当我们调试一个app的时候,可以看到控制台输出的内容,有一项是uploading …..就是上传我们的apk到这个文件夹,上传成功之后才开始安装。另一个重要的文件夹就是data文件夹了,这个文件夹里边都是一些包名,打开这些包名之后我们会看到这样的一些文件:

1.data/data/包名/shared_prefs

2.data/data/包名/databases

3.data/data/包名/files

4.data/data/包名/cache

我这里没往板子上拷apk,所以app目录为空,就不进去看了,看下data里头都有什么吧,

 如果打开过data文件,应该都知道这些文件夹是干什么用的,我们在使用sharedPreferenced的时候,将数据持久化存储于本地,其实就是存在这个文件中的xml文件里,我们App里边的数据库文件就存储于databases文件夹中,还有我们的普通数据存储在files中,缓存文件存储在cache文件夹中,存储在这里的文件我们都称之为内部存储。

随便进一个目录看看去,这个里头东西有点少,刚好对我们胃口。

2.外部存储

  外部存储才是我们平时操作最多的,外部存储一般就是我们上面看到的storage文件夹,当然也有可能是mnt文件夹,这个不同厂家有可能不一样。

  一般来说,在storage文件夹中有一个sdcard文件夹,这个文件夹中的文件又分为两类,一类是公有目录,还有一类是私有目录,其中的公有目录有九大类,比如DCIM、DOWNLOAD等这种系统为我们创建的文件夹,私有目录就是Android这个文件夹,这个文件夹打开之后里边有一个data文件夹,打开这个data文件夹,里边有许多包名组成的文件夹。

  说到这里,我想大家应该已经可以分清楚什么是内部存储什么是外部存储了吧?好,分清楚之后我们就要看看怎么来操作内部存储和外部存储了。

 操作外部存储首先需要以下权限:

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

当你申请了write权限,那么read权限默认也就通过啦

再判断状态:

1 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState))

外部存储的根目录

1 Environment.getExternalStorageDirectory()

内外部路径汇总一览

路径方法名所属
data/data/包名/shared_prefs   内部
data/data/包名/databases   内部
data/data/包名/files getFilesDir() 内部
data/data/包名/cache getCacheDir() 内部
/mnt 【外部存储】或者 /storage 【外部存储】 Environment.getExternalStorageDirectory() 外部根目录
/storage/<type> Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) 外部九大公有目录
/storage/sdcard/Android/data/包名/files getExternalFilesDir(Environment.DIRECTORY_MUSIC) 外部私有目录
/storage/sdcard/Android/data/包名/cache getExternalCacheDir() 外部缓存目录

 

 发现特点了吗朋友,无论外部内部,只有路径中有包名,那么就是私有的,而且是随着程序的卸载而被删除的,有包名的路径均是Context中的方法,而公有的路径均是Environment调用的。

啊,朋友,我在这里我要吐槽一下,恶心啊,真的恶心,其实就那么三个路径,就因为厂家不同,搞得乱七八糟的,有得简单就嵌套一层目录,有的居然还嵌套两层进去,比如上面九大公有目录我写的是/storage/<type>,有的是在/mnt/<type>下,然后usb路径也在/mnt/usb,有的干脆是全都在/storage下,反正大家记住这些分区概念就行,到时候直接调用相关API,打印一下看一下具体目录分支吧,我也没有好的办法总结全部厂商得目录,没有厂商会觉得自己有问题。

3.操作存储空间

  首先,经过上面的分析,大家已经明白了,什么是内部存储,什么是外部存储,以及这两种存储方式分别存储在什么位置,一般来说,我们不会自己去操作内部存储空间,没有root权限的话,我们也没法操作内部存储空间,事实上内部存储主要是由系统来维护的。不过在代码中我们是可以访问到这个文件夹的。由于内部存储空间有限,在开发中我们一般都是操作外部存储空间,Google官方建议我们App的数据应该存储在外部存储的私有目录中该App的包名下,这样当用户卸载掉App之后,相关的数据会一并删除,如果你直接在/storage/sdcard目录下创建了一个应用的文件夹,那么当你删除应用的时候,这个文件夹就不会被删除。

   大家看到,有包名的路径我们都是调用Context中的方法来获得,没有包名的路径,我们直接调用Environment中的方法获得,那么其中有两个方法需要传入一个String类型的参数,这个参数我们使用了Environment中的常量,参数的意思是我们要访问这个路径下的哪个文件夹,比如getExternalFilesDir方法,我们看看它的源码:

 1 /**
 2 *
 3 * @param type The type of files directory to return.  May be null for
 4 * the root of the files directory or one of
 5 * the following Environment constants for a subdirectory:
 6 * {@link android.os.Environment#DIRECTORY_MUSIC},
 7 * {@link android.os.Environment#DIRECTORY_PODCASTS},
 8 * {@link android.os.Environment#DIRECTORY_RINGTONES},
 9 * {@link android.os.Environment#DIRECTORY_ALARMS},
10 * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
11 * {@link android.os.Environment#DIRECTORY_PICTURES}, or
12 * {@link android.os.Environment#DIRECTORY_MOVIES}.
13 *
14 * @return The path of the directory holding application files
15 * on external storage.  Returns null if external storage is not currently
16 * mounted so it could not ensure the path exists; you will need to call
17 * this method again when it is available.
18 *
19 * @see #getFilesDir
20 * @see android.os.Environment#getExternalStoragePublicDirectory
21 */
22 @Nullable
23 public abstract File getExternalFilesDir(@Nullable String type);

  它的注释非常多,我这里只列出其中一部分,我们看到,我们可以访问files文件夹下的Music文件夹、Movies文件夹等等好几种。

  说到这里,我想大家对内部存储、外部存储该有了一个清晰的认识了吧。我们在开发中,不建议往内部存储中写太多的数据,毕竟空间有限。外部存储在使用的时候最好能够将文件存放在私有目录下,这样有利于系统维护,也避免用户的反感。

  现在我们再来看看我们一开始提出的问题,当我们点击清除数据的时候清除的是哪里的数据呢?毫无疑问,当然是内部存储目录中相应的files和cache文件夹中的文件和外部存储中相应的files和cache文件夹中的文件,至于这些文件夹的路径我想你应该已经明白了。

 

SD卡路径

这个货真真是要了老命,这也是我安卓学习中见到过最SB的东西,一般的方法根本不好使,结合网上有的方法加上公司项目中的方法,总结如下:

百分百好用的获取SD卡路径方法:

 1       try {
 2             StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
 3             Method getVolumeList = null;
 4             getVolumeList = sm.getClass().getDeclaredMethod("getVolumeList");
 5             Object[] volumeList = (Object[]) getVolumeList.invoke(sm);
 6             for (Object volume : volumeList) {
 7                 Method getPath = volume.getClass().getDeclaredMethod("getPath");
 8                 Method isRemovable = volume.getClass().getDeclaredMethod("isRemovable");
 9                 String path = (String) getPath.invoke(volume);              
10                 boolean removable = (Boolean) isRemovable.invoke(volume);
11                 if (removable) {
12                     paths.add(path);
13                 }
14             }
15             for (String path : paths) {
16                 Log.d("qwer", "path = > " + path);
17             }
18         } catch (NoSuchMethodException e) {
19             e.printStackTrace();
20         } catch (InvocationTargetException e) {
21             e.printStackTrace();
22         } catch (IllegalAccessException e) {
23             e.printStackTrace();
24         }            

 

 最后集合 path 中的值就是SD卡根目录

 

1 /storage/sdcard1

 

 我这里的安卓样板是/mnt/usb/sda1,使具体情况而定吧

 虽然无视版本百分百好用,但是如果你的手机有SD卡槽却没插SD卡,该方法最后 path 返回的是 null ,也就是说该方法无法判断到底是没插SD卡还是根本不支持SD卡

 其实还有一种方法

1 String path = System.getenv("SECONDARY_STORAGE");

 

该方法只要你手机支持SD卡,无论你插没插SD卡,均会返回SD卡路径,但是6.0及以上该方法被移除

Environment中源码其实就是根据这个方法获取路径的

安卓官方文档大家一定要看,他就是我们开发者的权威呀,圣经呀!!

Android外部存储/内部存储路径获取大全

 1 public class DirectoryUtils {
 2     private static final String TAG = "DirectoryUtils";
 3     public static void getEnvironmentDirectories() {
 4         //:/system
 5         String rootDir = Environment.getRootDirectory().toString();
 6         System.out.println("Environment.getRootDirectory()=:" + rootDir);
 7         //:/data 用户数据目录
 8         String dataDir = Environment.getDataDirectory().toString();
 9         System.out.println("Environment.getDataDirectory()=:" + dataDir);
10         //:/cache 下载缓存内容目录
11         String cacheDir = Environment.getDownloadCacheDirectory().toString();
12         System.out.println("Environment.getDownloadCacheDirectory()=:" + cacheDir);
13         //:/mnt/sdcard或者/storage/emulated/0或者/storage/sdcard0 主要的外部存储目录
14         //这个不一定是外部存储
15         String storageDir = Environment.getExternalStorageDirectory().toString();
16         System.out.println("Environment.getExternalStorageDirectory()=:" + storageDir);
17         //:/mnt/sdcard/Pictures或者/storage/emulated/0/Pictures或者/storage/sdcard0/Pictures
18         String publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
19         System.out.println("Environment.getExternalStoragePublicDirectory()=:" + publicDir);
20         //获取SD卡是否存在:mounted
21         String storageState = Environment.getExternalStorageState().toLowerCase();
22         System.out.println("Environment.getExternalStorageState()=:" + storageState);
23         //设备的外存是否是用内存模拟的,是则返回true。(API Level 11)
24         boolean isEmulated = Environment.isExternalStorageEmulated();
25         System.out.println("Environment.isExternalStorageEmulated()=:" + isEmulated);
26         //设备的外存是否是可以拆卸的,比如SD卡,是则返回true。(API Level 9)
27         boolean isRemovable = Environment.isExternalStorageRemovable();
28         System.out.println("Environment.isExternalStorageRemovable()=:" + isRemovable);
29     }
30     public static void getApplicationDirectories(Context context) {
31         //获取当前程序路径 应用在内存上的目录 :/data/data/com.mufeng.toolproject/files
32         String filesDir = context.getFilesDir().toString();
33         System.out.println("context.getFilesDir()=:" + filesDir);
34         //应用的在内存上的缓存目录 :/data/data/com.mufeng.toolproject/cache
35         String cacheDir = context.getCacheDir().toString();
36         System.out.println("context.getCacheDir()=:" + cacheDir);
37         //应用在外部存储上的目录 :/storage/emulated/0/Android/data/com.mufeng.toolproject/files/Movies
38         String externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES).toString();
39         System.out.println("context.getExternalFilesDir()=:" + externalFilesDir);
40         //应用的在外部存储上的缓存目录 :/storage/emulated/0/Android/data/com.mufeng.toolproject/cache
41         String externalCacheDir = context.getExternalCacheDir().toString();
42         System.out.println("context.getExternalCacheDir()=:" + externalCacheDir);
43         //获取该程序的安装包路径 :/data/app/com.mufeng.toolproject-3.apk
44         String packageResourcePath = context.getPackageResourcePath();
45         System.out.println("context.getPackageResourcePath()=:" + packageResourcePath);
46         //获取程序默认数据库路径 :/data/data/com.mufeng.toolproject/databases/mufeng
47         String databasePat = context.getDatabasePath("mufeng").toString();
48         System.out.println("context.getDatabasePath(\"mufeng\")=:" + databasePat);
49     }
50 }

 好了,最后再送给大家一个文件操作工具类:

  1 public class SDCardHelper {
  2  
  3      // 判断SD卡是否被挂载
  4      public static boolean isSDCardMounted() {
  5          // return Environment.getExternalStorageState().equals("mounted");
  6          return Environment.getExternalStorageState().equals(
  7          Environment.MEDIA_MOUNTED);
  8     }
  9  
 10     // 获取SD卡的根目录
 11     public static String getSDCardBaseDir() {
 12          if (isSDCardMounted()) {
 13                return Environment.getExternalStorageDirectory().getAbsolutePath();
 14          }
 15          return null;
 16     }
 17  
 18     // 获取SD卡的完整空间大小,返回MB
 19     public static long getSDCardSize() {
 20          if (isSDCardMounted()) {
 21               StatFs fs = new StatFs(getSDCardBaseDir());
 22               long count = fs.getBlockCountLong();
 23               long size = fs.getBlockSizeLong();
 24               return count * size / 1024 / 1024;
 25          }
 26          return 0;
 27     }
 28  
 29     // 获取SD卡的剩余空间大小
 30     public static long getSDCardFreeSize() {
 31          if (isSDCardMounted()) {
 32                StatFs fs = new StatFs(getSDCardBaseDir());
 33                long count = fs.getFreeBlocksLong();
 34                long size = fs.getBlockSizeLong();
 35                return count * size / 1024 / 1024;
 36          }
 37          return 0;
 38     }
 39  
 40     // 获取SD卡的可用空间大小
 41     public static long getSDCardAvailableSize() {
 42          if (isSDCardMounted()) {
 43                StatFs fs = new StatFs(getSDCardBaseDir());
 44                long count = fs.getAvailableBlocksLong();
 45                long size = fs.getBlockSizeLong();
 46                return count * size / 1024 / 1024;
 47          }
 48          return 0;
 49     }
 50  
 51     // 往SD卡的公有目录下保存文件
 52     public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
 53          BufferedOutputStream bos = null;
 54          if (isSDCardMounted()) {
 55                File file = Environment.getExternalStoragePublicDirectory(type);
 56                try {
 57                     bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
 58                     bos.write(data);
 59                     bos.flush();
 60                     return true;
 61                } catch (Exception e) {
 62                     e.printStackTrace();
 63                } finally {
 64                     try {
 65                           bos.close();
 66                     } catch (IOException e) {
 67                           // TODO Auto-generated catch block
 68                           e.printStackTrace();
 69                     }
 70                }
 71           }
 72           return false;
 73      }
 74  
 75      // 往SD卡的自定义目录下保存文件
 76      public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
 77           BufferedOutputStream bos = null;
 78           if (isSDCardMounted()) {
 79                 File file = new File(getSDCardBaseDir() + File.separator + dir);
 80                 if (!file.exists()) {
 81                       file.mkdirs();// 递归创建自定义目录
 82                 }
 83                 try {
 84                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
 85                       bos.write(data);
 86                       bos.flush();
 87                       return true;
 88                 } catch (Exception e) {
 89                       e.printStackTrace();
 90                 } finally {
 91                       try {
 92                             bos.close();
 93                       } catch (IOException e) {
 94                             // TODO Auto-generated catch block
 95                             e.printStackTrace();
 96                       }
 97                 }
 98            }
 99            return false;
100      }
101  
102      // 往SD卡的私有Files目录下保存文件
103      public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context) {
104          BufferedOutputStream bos = null;
105          if (isSDCardMounted()) {
106                File file = context.getExternalFilesDir(type);
107                try {
108                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
109                       bos.write(data);
110                       bos.flush();
111                       return true;
112                } catch (Exception e) {
113                       e.printStackTrace();
114                } finally {
115                       try {
116                             bos.close();
117                       } catch (IOException e) {
118                             // TODO Auto-generated catch block
119                             e.printStackTrace();
120                       }
121                }
122           }
123           return false;
124      }
125  
126      // 往SD卡的私有Cache目录下保存文件
127      public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
128           BufferedOutputStream bos = null;
129           if (isSDCardMounted()) {
130                 File file = context.getExternalCacheDir();
131                 try {
132                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
133                       bos.write(data);
134                       bos.flush();
135                       return true;
136                 } catch (Exception e) {
137                       e.printStackTrace();
138                 } finally {
139                       try {
140                             bos.close();
141                       } catch (IOException e) {
142                             // TODO Auto-generated catch block
143                             e.printStackTrace();
144                       }
145                }
146           }
147           return false;
148      }
149  
150      // 保存bitmap图片到SDCard的私有Cache目录
151      public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
152           if (isSDCardMounted()) {
153                 BufferedOutputStream bos = null;
154                 // 获取私有的Cache缓存目录
155                 File file = context.getExternalCacheDir();
156  
157                 try {
158                        bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
159                        if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))) {
160                               bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
161                        } else {
162                               bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
163                        }
164                        bos.flush();
165                 } catch (Exception e) {
166                        e.printStackTrace();
167                 } finally {
168                        if (bos != null) {
169                             try {
170                                  bos.close();
171                             } catch (IOException e) {
172                                  e.printStackTrace();
173                             }
174                        }
175                  }
176                  return true;
177           } else {
178                 return false;
179           }
180      }
181  
182      // 从SD卡获取文件
183      public static byte[] loadFileFromSDCard(String fileDir) {
184           BufferedInputStream bis = null;
185           ByteArrayOutputStream baos = new ByteArrayOutputStream();
186  
187           try {
188                 bis = new BufferedInputStream(new FileInputStream(new File(fileDir)));
189                 byte[] buffer = new byte[8 * 1024];
190                 int c = 0;
191                 while ((c = bis.read(buffer)) != -1) {
192                      baos.write(buffer, 0, c);
193                      baos.flush();
194                 }
195                 return baos.toByteArray();
196           } catch (Exception e) {
197                 e.printStackTrace();
198           } finally {
199                 try {
200                      baos.close();
201                      bis.close();
202                 } catch (IOException e) {
203                      e.printStackTrace();
204                 }
205           }
206           return null;
207      }
208  
209      // 从SDCard中寻找指定目录下的文件,返回Bitmap
210      public Bitmap loadBitmapFromSDCard(String filePath) {
211           byte[] data = loadFileFromSDCard(filePath);
212           if (data != null) {
213                Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
214                if (bm != null) {
215                      return bm;
216                }
217           }
218           return null;
219      }
220  
221      // 获取SD卡公有目录的路径
222      public static String getSDCardPublicDir(String type) {
223           return Environment.getExternalStoragePublicDirectory(type).toString();
224      }
225  
226      // 获取SD卡私有Cache目录的路径
227      public static String getSDCardPrivateCacheDir(Context context) {
228           return context.getExternalCacheDir().getAbsolutePath();
229      }
230  
231      // 获取SD卡私有Files目录的路径
232      public static String getSDCardPrivateFilesDir(Context context, String type) {
233           return context.getExternalFilesDir(type).getAbsolutePath();
234      }
235  
236      public static boolean isFileExist(String filePath) {
237           File file = new File(filePath);
238           return file.isFile();
239      }
240  
241      // 从sdcard中删除文件
242      public static boolean removeFileFromSDCard(String filePath) {
243           File file = new File(filePath);
244           if (file.exists()) {
245                try {
246                      file.delete();
247                      return true;
248                } catch (Exception e) {
249                      return false;
250                }
251           } else {
252                return false;
253           }
254      }
255 }
posted on 2019-09-21 15:31  Reverse-xiaoyu  阅读(1295)  评论(0编辑  收藏  举报