在Android開發過程中,我們經常會對檔案系統進行操作——存放、釋放我們應用的資料。Android系統中提供了各種功能的檔案目錄,每個目錄都有相應的特點和功能。 這篇文章主要介紹和記錄一下實際開發過程中常用的各個檔案目錄。
(1)内部存儲(Internal Storage)
内部存儲是App的私有目錄,當一個應用解除安裝之後,内部存儲中的這些檔案也被删除。Shared Preferences和SQLite資料庫檔案都是存儲在内部存儲空間上的。
-context.getFileDir()
路徑:(data/data/應用包名/files),
不過經實際測試(華為、小米手機等),getFileDir實際路徑為: /data/user/0/ 應用包名/files
Log.e(TAG,getApplicationContext().getFilesDir().getAbsolutePath())
-context.getCacheDir()
應用程式的緩存目錄,該目錄内的檔案在裝置記憶體不足時會優先被删除掉,是以存放在這裡的檔案是沒有任何保障的,可能會随時丢掉。
路徑:(data/data/應用包名/cache),不過經實際測試(華為、小米手機等),getCacheDir的手機路徑為: /data//data/user/0/應用包名/cache
Log.e(TAG,getApplicationContext().getCacheDir().getAbsolutePath())
(2 )外部存儲(External Storage)
外部存儲一般是指SDCard,外部存儲中的檔案是可以被使用者或者其他應用程式修改的,外部存儲中的目錄實際上也分為兩種:
-公共檔案(目錄)
檔案是可以被自由通路,且檔案的資料對其他應用或者使用者來說都是由 意義的,當應用被解除安裝之後,其解除安裝前建立的檔案仍然保留。
Environment.getExternalStorageDirectory()
SD卡的根目錄,路徑:/storage/emulated/0
Log.e(TAG, Environment.getExternalStorageDirectory().getAbsolutePath());
-私有檔案
由于是外部存儲的原因即使是這種類型的檔案也能被其他程式通路,隻不過一個應用私有的檔案對其他應用其實是沒有通路價值的(惡意程式除外)。外部存儲上,應用私有檔案的價值在于解除安裝之後,這些檔案也會被删除。類似于内部存儲。
getApplicationContext().getExternalFilesDir(“”)
路徑:/storage/emulated/0/Android/data/應用包名/files
Log.e(TAG, getApplicationContext().getExternalFilesDir("").getAbsolutePath());
擴充: getExternalFilesDir是手機中設定 → 應用 → 具體應用詳情→ 清除資料 的操作對象
getApplicationContext().getExternalCacheDir()
路徑:/storage/emulated/0/Android/data/應用包名/cache
Log.e(TAG,getApplicationContext().getExternalCacheDir().getAbsolutePath());
擴充:getExternalCacheDir是手機中設定 → 應用 → 具體應用詳情→ 清除緩存的操作對象
以上這篇Android 擷取系統各個目錄的方法就是小編分享給大家的全部内容了,希望能給大家一個參考,也希望大家多多支援腳本之家。