内部存储,我们称为InternalStorage,外部存储我们称为ExternalStorage
首先我们打开DDMS,有一个File Explorer,如下:
这里有三个文件夹需要我们重视,一个是data,一个是mnt,一个是storage,我们下面就详细说说这三个文件夹。
1.内部存储
data文件夹就是我们常说的内部存储,当我们打开data文件夹之后(没有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
如果打开过data文件,应该都知道这些文件夹是干什么用的,我们在使用sharedPreferenced的时候,将数据持久化存储于本地,其实就是存在这个文件中的xml文件里,我们App里边的数据库文件就存储于databases文件夹中,还有我们的普通数据存储在files中,缓存文件存储在cache文件夹中,存储在这里的文件我们都称之为内部存储。
External Storage
由于Android系统的厂商比较多,对于外部存储目录的定义有所不同,可能在根目录下的mnt,sdcard和storage下。以storage为例,打开emulated/0目录,外部存储目录就出现了。虽然可以通过多种路径打开外部存储文件,但是最终他们的路径是相同的:
外部存储目录下包含两大类:公有目录和私有目录
- 公有目录:包括九大系统创建的文件夹:Download,DICM之类的
- 私有目录:路径为/Android/data/package name/。这里包含了上述的九大文件和cache文件。
3.操作存储空间
首先,经过上面的分析,大家已经明白了,什么是内部存储,什么是外部存储,以及这两种存储方式分别存储在什么位置,一般来说,我们不会自己去操作内部存储空间,没有root权限的话,我们也没法操作内部存储空间,事实上内部存储主要是由系统来维护的。不过在代码中我们是可以访问到这个文件夹的。由于内部存储空间有限,在开发中我们一般都是操作外部存储空间,Google官方建议我们App的数据应该存储在外部存储的私有目录中该App的包名下,这样当用户卸载掉App之后,相关的数据会一并删除,如果你直接在/storage/sdcard目录下创建了一个应用的文件夹,那么当你删除应用的时候,这个文件夹就不会被删除。
经过以上的介绍,我们可以总结出下面一个表格:
一目了然,什么是内部存储,什么是外部存储。
如果按照路径的特征,我们又可以将文件存储的路径分为两大类,一类是路径中含有包名的,一类是路径中不含有包名的,含有包名的路径,因为和某个App有关,所以对这些文件夹的访问都是调用Context里边的方法,而不含有包名的路径,和某一个App无关,我们可以通过Environment中的方法来访问。如下图:
大家看到,有包名的路径我们都是调用Context中的方法来获得,没有包名的路径,我们直接调用Environment中的方法获得,那么其中有两个方法需要传入一个String类型的参数,这个参数我们使用了Environment中的常量,参数的意思是我们要访问这个路径下的哪个文件夹,比如getExternalFilesDir方法,我们看看它的源码:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Nullable
- public abstract File getExternalFilesDir(@Nullable String type);
它的注释非常多,我这里只列出其中一部分,我们看到,我们可以访问files文件夹下的Music文件夹、Movies文件夹等等好几种。
说到这里,我想大家对内部存储、外部存储该有了一个清晰的认识了吧。我们在开发中,不建议往内部存储中写太多的数据,毕竟空间有限。外部存储在使用的时候最好能够将文件存放在私有目录下,这样有利于系统维护,也避免用户的反感。
现在我们再来看看我们一开始提出的问题,当我们点击清除数据的时候清除的是哪里的数据呢?毫无疑问,当然是内部存储目录中相应的files和cache文件夹中的文件和外部存储中相应的files和cache文件夹中的文件,至于这些文件夹的路径我想你应该已经明白了。
两个对比:
External Storage Private.png
对比项 | Internal Storage | External Storage |
---|
可信度 | 永远可用(Permanent) | 可能不可用,最典型的当设备作为USB存储被mount时不可用 |
访问权限 | App存储内容仅App本身(或共享uid的App)可访问(Root除外) | App存储内容全局可读 |
内容持久 | App存储内容随App卸载而消失 | 当App卸载时,只有存在Android/data/package name/路径下的文件会消失 |
适用情况 | 存储内容仅App自己访问时的最佳选择 | 存储内容希望与其他App共享或传到电脑上,但是不想申请任何权限时的最佳选择 |
External Storage存储数据
首先我们要获取外部存储目标文件的路径:
目标目录 | 获取方法 |
---|
公有目录九大文件 | Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) |
公有根目录 | Environment.getExternalStorageDirectory() |
私有目录file | Context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) |
私有目录cache | Context.getExternalCacheDir() |
个人总结:删除app会删除内部存储,即
/data/data/package name下回被删除,和外部存储的Android/data/package name/路径下的文件会消失。
清除缓存和清除数据:
清除缓存应该是:为了在清除缓存的时候能够正常清除与应用相关的缓存,请将缓存文件存放在getCacheDir()或者 getExternalCacheDir()路径下。
清除数据应该是:比如SharedPreferences、数据库等等,这些数据都是在程序运行过程中保存的用户配置信息,清除数据后。
参考:http://www.jianshu.com/p/bc817c520aad
记得申请权限
- <uses-permission
- android:name="android.permission.WRITE_EXTERNAL_STORAGE">
- </uses-permission>
好了,最后再送给大家一个文件操作工具类:
- public class SDCardHelper {
-
-
- public static boolean isSDCardMounted() {
-
- return Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED);
- }
-
-
- public static String getSDCardBaseDir() {
- if (isSDCardMounted()) {
- return Environment.getExternalStorageDirectory().getAbsolutePath();
- }
- return null;
- }
-
-
- public static long getSDCardSize() {
- if (isSDCardMounted()) {
- StatFs fs = new StatFs(getSDCardBaseDir());
- long count = fs.getBlockCountLong();
- long size = fs.getBlockSizeLong();
- return count * size / 1024 / 1024;
- }
- return 0;
- }
-
-
- public static long getSDCardFreeSize() {
- if (isSDCardMounted()) {
- StatFs fs = new StatFs(getSDCardBaseDir());
- long count = fs.getFreeBlocksLong();
- long size = fs.getBlockSizeLong();
- return count * size / 1024 / 1024;
- }
- return 0;
- }
-
-
- public static long getSDCardAvailableSize() {
- if (isSDCardMounted()) {
- StatFs fs = new StatFs(getSDCardBaseDir());
- long count = fs.getAvailableBlocksLong();
- long size = fs.getBlockSizeLong();
- return count * size / 1024 / 1024;
- }
- return 0;
- }
-
-
- public static boolean saveFileToSDCardPublicDir(byte[] data, String type,
- String fileName) {
- BufferedOutputStream bos = null;
- if (isSDCardMounted()) {
- File file = Environment.getExternalStoragePublicDirectory(type);
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(
- file, fileName)));
- bos.write(data);
- bos.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- bos.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
- return false;
- }
-
-
- public static boolean saveFileToSDCardCustomDir(byte[] data, String dir,
- String fileName) {
- BufferedOutputStream bos = null;
- if (isSDCardMounted()) {
- File file = new File(getSDCardBaseDir() + File.separator + dir);
- if (!file.exists()) {
- file.mkdirs();
- }
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(
- file, fileName)));
- bos.write(data);
- bos.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- bos.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
- return false;
- }
-
-
- public static boolean saveFileToSDCardPrivateFilesDir(byte[] data,
- String type, String fileName, Context context) {
- BufferedOutputStream bos = null;
- if (isSDCardMounted()) {
- File file = context.getExternalFilesDir(type);
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(
- file, fileName)));
- bos.write(data);
- bos.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- bos.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
- return false;
- }
-
-
- public static boolean saveFileToSDCardPrivateCacheDir(byte[] data,
- String fileName, Context context) {
- BufferedOutputStream bos = null;
- if (isSDCardMounted()) {
- File file = context.getExternalCacheDir();
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(
- file, fileName)));
- bos.write(data);
- bos.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- bos.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
- return false;
- }
-
-
- public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap,
- String fileName, Context context) {
- if (isSDCardMounted()) {
- BufferedOutputStream bos = null;
-
- File file = context.getExternalCacheDir();
-
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(
- file, fileName)));
- if (fileName != null
- && (fileName.contains(".png") || fileName
- .contains(".PNG"))) {
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
- } else {
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- }
- bos.flush();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bos != null) {
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return true;
- } else {
- return false;
- }
- }
-
-
- public static byte[] loadFileFromSDCard(String fileDir) {
- BufferedInputStream bis = null;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- try {
- bis = new BufferedInputStream(
- new FileInputStream(new File(fileDir)));
- byte[] buffer = new byte[8 * 1024];
- int c = 0;
- while ((c = bis.read(buffer)) != -1) {
- baos.write(buffer, 0, c);
- baos.flush();
- }
- return baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- baos.close();
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
-
-
- public Bitmap loadBitmapFromSDCard(String filePath) {
- byte[] data = loadFileFromSDCard(filePath);
- if (data != null) {
- Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
- if (bm != null) {
- return bm;
- }
- }
- return null;
- }
-
-
- public static String getSDCardPublicDir(String type) {
- return Environment.getExternalStoragePublicDirectory(type).toString();
- }
-
-
- public static String getSDCardPrivateCacheDir(Context context) {
- return context.getExternalCacheDir().getAbsolutePath();
- }
-
-
- public static String getSDCardPrivateFilesDir(Context context, String type) {
- return context.getExternalFilesDir(type).getAbsolutePath();
- }
-
- public static boolean isFileExist(String filePath) {
- File file = new File(filePath);
- return file.isFile();
- }
-
-
- public static boolean removeFileFromSDCard(String filePath) {
- File file = new File(filePath);
- if (file.exists()) {
- try {
- file.delete();
- return true;
- } catch (Exception e) {
- return false;
- }
- } else {
- return false;
- }
- }
- }
我自己的测试:
Log.e("路径:内部存储的File绝对路径", MainActivity.this.getFilesDir().getAbsolutePath());
Log.e("路径:内部存储的Cache绝对路径", MainActivity.this.getCacheDir().getAbsolutePath());
Log.e("路径:外部存储的绝对路径", Environment.getExternalStorageDirectory().getAbsolutePath());
Log.e("路径:外部存储的相对路径", Environment.getExternalStorageDirectory().getPath());
Log.e("路径:外部存储的公用路径", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
Log.e("路径:外部存储的私有file路径", MainActivity.this.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath());
Log.e("路径:外部存储的私有cache路径", MainActivity.this.getExternalCacheDir().getAbsolutePath());
得到的数据:验证
路径:内部存储的File绝对路径: /data/data/pae.justcan.com.testemail/files
路径:内部存储的Cache绝对路径: /data/data/pae.justcan.com.testemail/cache
路径:外部存储的绝对路径: /storage/emulated/0
路径:外部存储的相对路径: /storage/emulated/0
路径:外部存储的公用路径: /storage/emulated/0/Music
路径:外部存储的私有file路径: /storage/emulated/0/Android/data/pae.justcan.com.testemail/files/Music
路径:外部存储的私有cache路径: /storage/emulated/0/Android/data/pae.justcan.com.testemail/cache