概述
Android存盤在6.0之前,真的可以用“混亂不堪”來形容,
造成的原因很多,個人認為有以下幾點:
- android早期手機本身的記憶體很小,大部分app的讀寫都在sd卡內操作,
- 用戶權限意識薄弱,android 6.0之前直接在安裝時申請權限,用戶往往在未知情況下就給予了存盤權限,
- app為了使自己的應用資料好看,直接將資源存盤到sd卡,存盤在sd卡的檔案將不會計算成該app的記憶體占用,
- app處于賣方市場,如果不給存盤權限用戶就無法使用app部分甚至全部功能,
目前,隨著Android越發完善,官方也有了自己推薦的存盤方案,作為一個android開發者,非常有比較了解一下,
官方檔案:訪問應用專屬檔案
筆者根據自己的日常使用經驗以及官方檔案做本文總結,如果有理解有誤的地方歡迎評論交流,
存盤方案優劣
內部存盤
只有自己可以訪問,其他應用不能訪問,自己訪問不需要權限,
android api一般是:context.getCacheDir() 和 context.getFilesDir()
使用場景
- 小程式、小游戲
(持久檔案,適合存盤在context.getFilesDir())
與app相關聯,app卸載后就沒有用了, - 快取圖片、視頻
(臨時檔案,適合存盤在context.getCacheDir())
隨時可以清理掉,清理掉后就是走冷啟動邏輯,
優勢
- 不需要申請權限,用戶無感知,
- 不會影響手機存盤,app洗掉,即相關資源一起洗掉,
劣勢
- 無法永久化存盤,app洗掉后會跟著洗掉,
- android手機存盤不夠時,應用的快取檔案會被洗掉,回收空間,
外部存盤
獲得讀寫權限后,大家都可以讀寫,
android api一般是:Environment.getExternalStorageDirectory()
使用場景
- 照片、視頻、檔案等本地存盤
需要放到外存,便于用戶分享和使用,
優勢
- 除非用戶主動洗掉,否則可以一直存盤在用戶手機中,
(這個用法很多,比如多個應用可以共享一份資源,不用每個應用都維護一份;當應用洗掉后第二次安裝,能保留部分用戶資料以及快取) - 獲得權限后也意味著可以讀取手機中的其他內容,如照片,視頻等,
劣勢
- 需要申請用戶權限,部分用戶會選擇拒絕,
- sd卡取出后,就無法訪問,
這個問題主要在較老的機型上,目前大部分手機都沒有sd卡了, - 由于app被洗掉時外存中的資料不會自動被洗掉,因此就會成為用戶的“存盤垃圾”,
常用存盤方案
代碼
private void testAndroidFile() {
Log.d(TAG, "Environment.getDataDirectory() filepath:" + Environment.getDataDirectory());
Log.d(TAG, "Environment.getDownloadCacheDirectory() filepath:"
+ Environment.getDownloadCacheDirectory());
Log.d(TAG, "Environment.getRootDirectory() filepath:" + Environment.getRootDirectory());
Log.d(TAG, "Environment.getExternalStorageDirectory() filepath:"
+ Environment.getExternalStorageDirectory());
Log.d(TAG, "context.getCacheDir() filepath:" + getCacheDir());
Log.d(TAG, "context.getDataDir() filepath:" + getDataDir());
Log.d(TAG, "context.getFilesDir() filepath:" + getFilesDir());
Log.d(TAG, "context.getObbDir() filepath:" + getObbDir());
Log.d(TAG, "context.getNoBackupFilesDir() filepath:" + getNoBackupFilesDir());
Log.d(TAG, "context.getCodeCacheDir() filepath:" + getCodeCacheDir());
Log.d(TAG, "context.getExternalCacheDir() filepath:" + getExternalCacheDir());
Log.d(TAG, "context.getExternalFilesDir(null) filepath:" + getExternalFilesDir(null));
}
列印結果(手機:小米9、Android10、MIUI 12)
Environment.getDataDirectory() filepath:/data
Environment.getDownloadCacheDirectory() filepath:/data/cache
Environment.getRootDirectory() filepath:/system
Environment.getExternalStorageDirectory() filepath:/storage/emulated/0
context.getCacheDir() filepath:/data/user/0/com.example.androidfiletest/cache
context.getDataDir() filepath:/data/user/0/com.example.androidfiletest
context.getFilesDir() filepath:/data/user/0/com.example.androidfiletest/files
context.getObbDir() filepath:/storage/emulated/0/Android/obb/com.example.androidfiletest
context.getNoBackupFilesDir() filepath:/data/user/0/com.example.androidfiletest/no_backup
context.getCodeCacheDir() filepath:/data/user/0/com.example.androidfiletest/code_cache
context.getExternalCacheDir() filepath:/storage/emulated/0/Android/data/com.example.androidfiletest/cache
context.getExternalFilesDir(null) filepath:/storage/emulated/0/Android/data/com.example.androidfiletest/files
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/194882.html
標籤:其他
