Android中有各種各樣的數(shù)據(jù)樊销,大體上包括兩種獲取路徑的方式,一是通過(guò)Environment,二是通過(guò)Context层坠。
以下代碼放在MainActivity.java中,即可打印出以下不同種類的路徑刁笙。
/**
* 通過(guò)Environment獲取
*/
//獲得根目錄/data內(nèi)部存儲(chǔ)路徑
System.out.println("Environment.getDataDirectory(): " + Environment.getDataDirectory().getPath());
//獲得緩存目錄/cache
System.out.println("Environment.getDownloadCacheDirectory(): " + Environment.getDownloadCacheDirectory().getPath());
//獲得SD卡目錄/mnt/sdcard(獲取的是手機(jī)外置sd卡的路徑)
System.out.println("Environment.getExternalStorageDirectory(): " + Environment.getExternalStorageDirectory().getPath());
//獲得系統(tǒng)目錄/system
System.out.println("Environment.getRootDirectory(): " + Environment.getRootDirectory().getPath());
/**
* 通過(guò)Context獲取
*/
//用于獲取APP的cache目錄 /data/data/<application
System.out.println("getCacheDir(): " + getCacheDir().getPath());
//用于獲取APP的在SD卡中的cache目
System.out.println("getExternalCacheDir(): " + getExternalCacheDir().getPath());
//用于獲取APP的files目錄 /data/data/<application
System.out.println("getFilesDir(): " + getFilesDir());
//用于獲取APPSDK中的obb目錄
System.out.println("getObbDir: " + getObbDir().getPath());
//用于獲取APP的所在包目錄
System.out.println("getPackageName: " + getPackageName());
//來(lái)獲得當(dāng)前應(yīng)用程序?qū)?yīng)的 apk 文件的路徑
System.out.println("getPackageCodePath: " + getPackageCodePath());
//獲取該程序的安裝包路徑
System.out.println("getPackageResourcePath: " + getPackageResourcePath());
下面是結(jié)果輸出:
01-19 21:50:29.268 26495-26495/? I/System.out: Environment.getDataDirectory(): /data
01-19 21:50:29.269 26495-26495/? I/System.out: Environment.getDownloadCacheDirectory(): /data/cache
01-19 21:50:29.270 26495-26495/? I/System.out: Environment.getExternalStorageDirectory(): /storage/emulated/0
01-19 21:50:29.270 26495-26495/? I/System.out: Environment.getRootDirectory(): /system
01-19 21:50:29.270 26495-26495/? I/System.out: getCacheDir(): /data/user/0/com.ffh.pathdemo/cache
01-19 21:50:29.272 26495-26495/? I/System.out: getExternalCacheDir(): /storage/emulated/0/Android/data/com.ffh.pathdemo/cache
01-19 21:50:29.272 26495-26495/? I/System.out: getFilesDir(): /data/user/0/com.ffh.pathdemo/files
01-19 21:50:29.274 26495-26495/? I/System.out: getObbDir: /storage/emulated/0/Android/obb/com.ffh.pathdemo
01-19 21:50:29.274 26495-26495/? I/System.out: getPackageName: com.ffh.pathdemo
01-19 21:50:29.274 26495-26495/? I/System.out: getPackageCodePath: /data/app/com.ffh.pathdemo-1/base.apk
01-19 21:50:29.274 26495-26495/? I/System.out: getPackageResourcePath: /data/app/com.ffh.pathdemo-1/base.apk