Android中有各种各样的数据,大体上包括两种获取路径的方式,一是通过Environment,二是通过Context。
以下代码放在MainActivity.java中,即可打印出以下不同种类的路径。
/**
* 通过Environment获取
*/
//获得根目录/data内部存储路径
System.out.println("Environment.getDataDirectory(): " + Environment.getDataDirectory().getPath());
//获得缓存目录/cache
System.out.println("Environment.getDownloadCacheDirectory(): " + Environment.getDownloadCacheDirectory().getPath());
//获得SD卡目录/mnt/sdcard(获取的是手机外置sd卡的路径)
System.out.println("Environment.getExternalStorageDirectory(): " + Environment.getExternalStorageDirectory().getPath());
//获得系统目录/system
System.out.println("Environment.getRootDirectory(): " + Environment.getRootDirectory().getPath());
/**
* 通过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());
//来获得当前应用程序对应的 apk 文件的路径
System.out.println("getPackageCodePath: " + getPackageCodePath());
//获取该程序的安装包路径
System.out.println("getPackageResourcePath: " + getPackageResourcePath());
下面是结果输出:
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
网友评论