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.