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.

android-文件存储,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。