路徑方法 | Win | Android |
---|---|---|
GetHomePath | C:\Users\ggggcexx\AppData\Roaming | /data/user/0/com.stella.scan/files/ |
GetDocumentsPath | C:\Users\ggggcexx\Documents | /data/user/0/com.embarcadero.FirstAidExpert_FMX_D11/files |
GetSharedDocumentsPath | C:\Users\Public\Documents | /storage/emulated/0/Documents |
GetLibraryPath | C:\MyFiles\OrangeUIProduct\項目名稱\APP\Win32\Debug\ | /data/app/com.embarcadero.FirstAidExpert_FMX_D11-wlgPkLmXf6nZlWnaSyFsnw==/lib/arm |
GetCachePath | C:\Users\ggggcexx\AppData\Local | /data/user/0/com.embarcadero.FirstAidExpert_FMX_D11/cache |
GetPublicPath | C:\ProgramData | /storage/emulated/0/Android/data/com.embarcadero.FirstAidExpert_FMX_D11/files |
GetPicturesPath | C:\Users\ggggcexx\Pictures | /storage/emulated/0/Android/data/com.embarcadero.FirstAidExpert_FMX_D11/files/Pictures |
GetSharedPicturesPath | C:\Users\Public\Pictures | /storage/emulated/0/Pictures |
GetCameraPath | C:\Users\ggggcexx\Pictures | /storage/emulated/0/Android/data/com.embarcadero.FirstAidExpert_FMX_D11/files/DCIM |
GetSharedCameraPath | C:\Users\Public\Pictures | /storage/emulated/0/DCIM |
GetDownloadsPath | C:\Users\ggggcexx\Downloads | /storage/emulated/0/Android/data/com.embarcadero.FirstAidExpert_FMX_D11/files/Download |
GetSharedDownloadsPath | C:\Users\Public\Downloads | /storage/emulated/0/Download |
Delphi 跨平臺路徑操作 TPath | ||
Delphi 有一個 System.IOUtils 單元, | ||
里面有一個 TPath,是一個跨平臺的路徑操作結構體, | ||
里面有獲取常用路徑的靜態方法(不需要創建直接可以調用的方法)。 |
GetDocumentsPath 就是應用的文檔目錄(私有存儲空間),
如果你要保存文件,一般將文件保存在文檔目錄。
在 Android 下,GetHomePath 和 GetDocumentsPath 都指向同一個目錄
GetCachePath 是緩存目錄,可以保存比如臨時下載的網絡圖片,
手機上的清除應用緩存就是清理的這個目錄。
還有一些帶 Shared 的目錄,
GetSharedDocumentsPath、GetSharedPicturesPath、GetSharedCameraPath、GetSharedDownloadsPath,
這些則是可以與其它應用共享的目錄,都保存在外部存儲空間中,
其他應用可以訪問到它們。
路徑分隔符
需要注意的是,安卓系統下面路徑分隔符和 Windows 下不一樣,
Windows 下是\,Anroid 下面是/,
那么我們平時寫代碼要拼路徑的時候,該怎么辦呢?
Delphi 已經幫我們考慮好了,
大家統一用常量 PathDelim 即可。
比如你要將當前登錄用戶的賬號密碼保存到 Config.ini 中,
那么你就可以通過 TPath.GetDocumentsPath+PathDelim+’Config.ini’來訪問它。
使用方法簡單
UserPath := System.IOUtils.TPATH.GetDocumentsPath + PathDelim + 'UserData.json';GroupPath := System.IOUtils.TPATH.GetDocumentsPath + PathDelim + 'GroupTable.json';PathDelim 代表PC與安卓的動態斜杠因為兩個客戶端文件路徑的斜杠是不一致的后面就加上報錯的文件名