安卓 getPath() 與 getAbsolutePath() 區別
先看一個示例:
Log.i("a", getFilesDir().toString()); Log.i("c", getFilesDir().getName()); Log.i("d", getFilesDir().getPath()); Log.i("b", getFilesDir().getAbsolutePath());
結果是:
I/a: /data/data/com.itpow.test/files
I/c: files
I/d:/data/data/com.itpow.test/files
I/b: /data/data/com.itpow.test/files
似乎 getPath() 與 getAbsolutePath() 並沒有區別,但是這只是在這種情況下,我們繼續看:
File file = new File(getFilesDir().getPath());
以上程式碼是沒有問題的,但是如果換成 getAbsolutePath() 就執行不起,還真奇了怪了。
有網友用 new File() 測試,具體是:如果 new File() 的引數是全路徑,則 getPath()、getAbsolutePath() 沒有區別,如果 new File() 的引數是相對路徑,則 getPath() 返回的是相對路徑(也就是引數),而 getAbsolutePath() 是轉換成全路徑後的全路徑。