android-文件存储

android 文件存储分为:internal storage(内部存储)和 External storage(外部存储例如sdcard)

internal storage:

1.内部存储一直存在

2.默认的只能被你自己的应用访问(当然通过设置 MODE_PRIVATE让其他应用访问而且其他应用要知道你的包名因为file存储的地址在/data/data/packname)下面。

3.当你删除此应用,这些文件也会被删除

4.访问内部存储不用manifest里面加权限

5.一般apk安装的在内部存储中当然也可以通过在manifest中设置android:installLocation设置apk安装的目录

6.内部目录分为filesDir和cacheDir 可以通过方法getFileDir()、getCacheDir()去获取他的地址

7.在内部存储放置的文件不用的要进行删除,如果一直不删除超出了系统分给你的大小,系统会在没有警告的自动删除你的存储的文件

8.内部文件的写入

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}
9.内部文件临时文件的写入


public File getTempFile(Context context, String url) {
    File file;
    try {
        String fileName = Uri.parse(url).getLastPathSegment();
        file = File.createTempFile(fileName, null, context.getCacheDir());
    catch (IOException e) {
        // Error while creating file
    }
    return file;
}

External Storage

1.外部文件不一定一直都在。

2.外部文件可以通过其他方式读取

3.当你删除应用是文件还是会存在

4.如果你向外部文件进行写入需要在manifest中加入权限:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE“

  如果你要读取尾外部文件到现在位置的版本是不需要加如权限的但是为了保证向后兼容最好在读文件时也加入权限:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 

备注:android:installLocation

Although apps are installed onto the internal storage by default, you can specify theandroid:installLocation attribute in your manifest so your app may be installed on external storage. Users appreciate this option when the APK size is very large and they have an external storage space that's larger than the internal storage. For more information, see App Install Location.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值