內部存盤器 介紹
The Storage Situation: Internal Storage
簡單來說Android的SDK認為內部存盤器是一個對于你的app來說特定的, 可以放檔案的目錄. 這個目錄下的檔案的app只能由你的app讀和寫, 其他的app將不能夠訪問這些檔案. 當然root權限例外.
Context中給你的獲取內部存盤器位置的方法....(sometimes):
-
getCacheDir() -
getDir() -
getDatabasePath() -
getFilesDir() -
openFileInput() -
openFileOutput()
其他的檔案存取操作都依賴于此.例如資料庫操作.
你的app的內部存盤一般在/data/data/your.application.package.name
在其他的情況下
如果你的程式員這樣寫:
File f = new File("/data/data/their.app.package.name/files/foo.txt");
這樣寫不僅需要打更多的字, 我們需要的目錄還不一定在這里.
當然, 我們有更好的代碼:
File f=new File(getFilesDir(), "foo.txt");
在android10中內部存盤器的操作還沒有發生改變.
Google's understand of "External Storage"
The Storage Situation: External Storage
外部存盤器 介紹
Android SDK檔案是這么對"外部存盤"定義的:
Every Android-compatible device supports a shared “external storage” that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer
官方檔案因為歷史原因把'the stuff that shows up when the user plugs their device into a computer using a USB cable'這個定義為"external storage"外部存盤, 但是Android4.4 又增加了可移動媒體(存盤?), 這也下面那篇文章講的東西.
這里提及的外部存盤的路徑都是由Environment.getExternalStorageDirectory().所指定的.但是, 這個方法在Api29(即android10)被棄用了.
在Android早期的版本, 內部存盤和外部存盤是相互分割的磁區, 但是在3.0之后, 內部存盤和外部存盤在相同的磁區,只是在不同的目錄里面, 然后硬體廠商也沒有繼續把內部存盤和外部存盤當作兩個分離的部分.(應該就是說現在的手機都是買來直接就是64GB, 128GB的, 不在物理上把內部和外部隔離了).
- For us as developers, the actual path under which this magical “external storage” was accessed varied over the years, from /sdcard, to locations under /storage, to eventually the current /mnt/shell/emulated/0.
所以說不能把目錄之類的東西hardcode進去
永遠不要硬編碼路徑
-
Environment.getExternalStorageDirectory() 可以獲取外部存盤的目錄just a big basket of random content
-
getExternalFilesDir()和getExternalCacheDir() 獲取的是app相對應的在外部存盤的目錄的兩個檔案夾.
-
Environment.getExternalStoragePublicDirectory() 集中放置的那些圖片, 電影的檔案.
Android規定的app的權限: Android1.5的時候, 有一個WRITE_EXTERNAL_STORAGE的permission, 在打開程式的時候, 回想用戶請求app打算修改外部存盤的內容, 任何app可以直接讀取外部存盤.
在Android4.4之后, 強制執行READ_EXTERNAL_STORAGE, 如果你沒有權限你無法讀取外部存盤.getExternalFilesDir() and getExternalCacheDir()這兩個方法甚至無需權限就可以直接讀寫.
android10 外部存盤也不給隨便創建檔案夾了, 目前android10可以臨時用一種方式來進行以前版本的操作,詳見 Temporarily opt-out of scoped storage
Google Thinks "Removable Storage"
The Storage Situation: Removable Storage
-
在大多數的設備上External Storage ≠ removable storage
-
在Android SDK中沒有提到移動存盤.
參考的一大段就是說在android4.4之后才有SDcard的支持,應用只能對自己的部分進行讀寫操作, 且無需權限, 并且通過檔案瀏覽器無需任何權限就可以對任何檔案進行操作.
在此之前和sdcard建立連接是通過各種黑科技.. 硬體廠商一套, 開發者自己又一套..
4.4之后只能讀, 而沒有寫的權限.
Google管這個叫secondary external storage
小結
總而言之android4.4到android10都可以在外部存盤應用程式自己新建一個檔案夾,用于備份等必要操作,而不用擔心在app卸載的時候被洗掉,而android10之后需要使用其他的方法進行適配,詳見
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/21785.html
標籤:Android
上一篇:Android實體資源
