05 Android基础--内部存储与外部存储

什么是内部存储,什么是外部存储?

1.内部存储与外部存储的存储介质:
内部存储的介质:RAM(内存) + 内部ROM
外部存储的介质:外部ROM + SDCard(TS卡等等)。

2.内部存储与外部存储的存储特点:

2.1一般来说,以/data开头的是内部存储。且内部存储不需要任何权限。
例如:

  • /data/data/< applicationId >/shared_prefs
  • /data/data/< applicationId >/databases
  • /data/data/< applicationId >/files // 通过context.getFilesDir() 获取该目录
  • /data/data/< applicationId >/cache //通过context.getCacheDir() 获取该目录

2.2 内部存储需要关注的文件夹:
app文件夹(未root无法打开):存放着所有app的apk文件夹
data文件夹:内部都是app的包名,存储着应用程序相关的数据,例如 data/data/包名/(shared_prefs、database、files、cache)

2.3 Android SDK提供了几个常见的内部存储文件的权限

  • Context.MODE_PRIVATE :私有方式存储,其他应用无法访问,覆盖旧的同名文件
  • Context.MODE_APPEND:私有方式存储,若有旧的同名文件,则在该文件上追加数据

2.4 一般来说,外部存储会放在storage文件夹下或者mnt文件夹下。且需要安卓的权限。
例如:
私有外部存储

  • /storage/emulated/0/Android/data/< applicationId >/files/Music //Context.getExternalFilesDir() 包含如Music等文件夹
  • /storage/emulated/0/Android/data/< applicationId >/cache //Context.getExternalCacheDir 外部缓存文件

以及共有外部存储

  • /storage/emulated/0 Environment.getExternalStorageDirectory()
  • /storage/emulated/0/Pictures Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)

2.5 外部存储需要注意的文件夹即外部存储的分类:
storage中有一个sdcard文件夹,sdcard下面可以分两类存储:

外部共有存储(共有目录):里面常见的有Pictures、Download等文件夹.

外部私有存储(私有目录):系统中的数据。

在这里插入图片描述

内部存储与外部存储的文件夹:
在这里插入图片描述

3.内部存储与外部存储,释放内存方面的总结:

  • 内部存储:随应用卸载被删除。
  • 外部存储:
    1.公有目录:存放一些下载的视频文件等,比如还有movies,fictures,music等公有的一些文件目录。
    2.私有目录:随应用卸载被删除。

内部存储与外部存储的代码示例

内部存储

				// 存
 				FileOutputStream fos = null;
                try {
                    //第一个参数:文件名
                    //第二个参数:表示文件输出的类型 这里选择Context.MODE_PRIVATE每次生成相同的文件名,则覆盖原有的文件
                    fos = openFileOutput('test', Context.MODE_PRIVATE);
                    String nameAndPassword = username + "." + password;
                    byte[] bytes = nameAndPassword.getBytes();
                    fos.write(bytes);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


				// 取
				FileInputStream fis = null;
                try {
                    fis = openFileInput(fileName);
                    //fis.available() 判断文件有多少个字节
                    byte[] bytes = new byte[fis.available()];
                    while (fis.read(bytes) != -1) {
                        String message = new String(bytes);
                        String[] split = message.split("\\.");
                        tv_message.setText("用户名:" + split[0] + "\n" + "密码:" + split[1]);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

外部存储

// 获取外部存储地址的位置并创建文件:
new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
				// 存
				OutputStream outputStream = null;
                try {
                   
                        outputStream = new FileOutputStream(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), 'test2'));
                        String nameAndPassword = username + "." + password;
                        byte[] bytes = nameAndPassword.getBytes();
                        outputStream.write(bytes);
                    
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

				// 取
				 FileInputStream fis = null;
                try {
                    //第一个参数:文件目录 第二个参数:文件名
                    //getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)对应的路径为:
                    // /storage/emulated/0/Android/data/com.example.customviewproject/files/Download
                    File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName);
                    
                        fis = new FileInputStream(file);
                        //判断当前文件的字节个数
                        byte[] bytes = new byte[fis.available()];
                        while (fis.read(bytes) != -1) {
                            String message = new String(bytes);
                            String[] split = message.split("\\.");
                            tv_message.setText("用户名:" + split[0] + "\n" + "密码:" + split[1]);
                        }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值