Android中的内部存储和外部存储

学而时习之,不亦说乎!

今天主要总结一下Android中APP的内部存储和外部存储

在Android开发中,我们很多时候会遇到内存、外部存储、内部存储。我们在android手机的设置里有清除数据和清除缓存,我想有一部分都不知道清除的到底是什么储存。


内存:顾名思义,就是手机里的应用运行的空间

1、内部存储

这里写图片描述

重要的文件夹就是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文件夹中,存储在这里的文件我们都称之为内部存储。

2、外部存储

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

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

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


如果你的api 版本低于8,那么不能使用getExternalFilesDir(),而是使用Environment.getExternalStorageDirectory()获得根路径之后,自己再想办法操作/Android/data//下的文件。
也就是说api 8以下的版本在操作文件的时候没有专门为私有文件和公共文件的操作提供api支持。你只能先获取根目录,然后自行想办法。

3、操作内存空间

这里写图片描述

一个操作工具类:

public class SDCardHelper {

     // 判断SD卡是否被挂载
     public static boolean isSDCardMounted() {
         // return Environment.getExternalStorageState().equals("mounted");
         return Environment.getExternalStorageState().equals(
         Environment.MEDIA_MOUNTED);
    }

    // 获取SD卡的根目录
    public static String getSDCardBaseDir() {
         if (isSDCardMounted()) {
               return Environment.getExternalStorageDirectory().getAbsolutePath();
         }
         return null;
    }

    // 获取SD卡的完整空间大小,返回MB
    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;
    }

    // 获取SD卡的剩余空间大小
    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;
    }

    // 获取SD卡的可用空间大小
    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;
    }

    // 往SD卡的公有目录下保存文件
    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) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                    }
               }
          }
          return false;
     }

     // 往SD卡的自定义目录下保存文件
     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) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
                }
           }
           return false;
     }

     // 往SD卡的私有Files目录下保存文件
     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) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
               }
          }
          return false;
     }

     // 往SD卡的私有Cache目录下保存文件
     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) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
               }
          }
          return false;
     }

     // 保存bitmap图片到SDCard的私有Cache目录
     public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
          if (isSDCardMounted()) {
                BufferedOutputStream bos = null;
                // 获取私有的Cache缓存目录
                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;
          }
     }

     // 从SD卡获取文件
     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;
     }

     // 从SDCard中寻找指定目录下的文件,返回Bitmap
     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;
     }

     // 获取SD卡公有目录的路径
     public static String getSDCardPublicDir(String type) {
          return Environment.getExternalStoragePublicDirectory(type).toString();
     }

     // 获取SD卡私有Cache目录的路径
     public static String getSDCardPrivateCacheDir(Context context) {
          return context.getExternalCacheDir().getAbsolutePath();
     }

     // 获取SD卡私有Files目录的路径
     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();
     }

     // 从sdcard中删除文件
     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;
          }
     }
}

小结:

清除数据主要是清除用户配置,比如SharedPreferences、数据库等等,这些数据都是在程序运行过程中保存的用户配置信息,清除数据后,下次进入程序就和第一次进入程序时一样

缓存是程序运行时的临时存储空间,它可以存放从网络下载的临时图片,从用户的角度出发清除缓存对用户并没有太大的影响,但是清除缓存后用户再次使用该APP时,由于本地缓存已经被清理,所有的数据需要重新从网络上获取。为了在清除缓存的时候能够正常清除与应用相关的缓存,请将缓存文件存放在getCacheDir()或者 getExternalCacheDir()路径下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值