一、Context 对象获取目录
1)getExternalFilesDir
说明
- 方法中的参数为需要在 files 目录下新建的目录名,如果新建的目录不存在,则会创建,反之,不会重复创建。
示例
// args
// Environment.DIRECTORY_MUSIC
// Environment.DIRECTORY_PODCASTS
// Environment.DIRECTORY_RINGTONES
// Environment.DIRECTORY_ALARMS
// Environment.DIRECTORY_NOTIFICATIONS
// Environment.DIRECTORY_PICTURES
// Environment.DIRECTORY_MOVIES
// Environment.DIRECTORY_DOWNLOADS
// Environment.DIRECTORY_DCIM
// /storage/emulated/0/Android/data/cn.opensrc.enterpriseopenaccountsdktest/files
File filePath1 = getExternalFilesDir("");
// /storage/emulated/0/Android/data/cn.opensrc.enterpriseopenaccountsdktest/files/DCIM
File filePath2 = getExternalFilesDir(Environment.DIRECTORY_DCIM);
// /storage/sdcard1/Android/data/cn.opensrc.enterpriseopenaccountsdktest/files/self
File filePath3 = getExternalFilesDir("self");
2)getExternalCacheDir
说明
示例
// /storage/emulated/0/Android/data/cn.opensrc.enterpriseopenaccountsdktest/cacheFile
filePath = getExternalCacheDir();
3)getCacheDir
示例
// /data/data/cn.opensrc.enterpriseopenaccountsdktest/cache
File filePath = getCacheDir();
4)getDir
说明
- 会新建名为 app_DirName 的目录,其中 app_ 为固定格式。
示例
// /data/data/cn.opensrc.enterpriseopenaccountsdktest/app_My
File filePath = getDir("My", MODE_PRIVATE);
5)getFilesDir
示例
// /data/data/cn.opensrc.enterpriseopenaccountsdktest/files
File filePath = getFilesDir();
6)getObbDir
说明
- 新建 obb 目录。应用卸载时此目录不会被卸载。
- obb 目录主要用来存放 obb 文件,通常游戏会用。
示例
// /storage/emulated/0/Android/obb/cn.opensrc.enterpriseopenaccountsdktest
File filePath = getObbDir();
二、Environment 静态方法获取目录
1)getExternalStorageDirectory
示例
// /storage/emulated/0
File filePath = Environment.getExternalStorageDirectory();
2)getDataDirectory
示例
// /data
File filePath = Environment.getDataDirectory();
3)getDownloadCacheDirectory
示例
// /cache
File filePath = Environment.getDownloadCacheDirectory();
4)getExternalStoragePublicDirectory
说明
- 返回的只是一个目录路径,而此路径目录并没有被创建。
示例
// args:
// Environment.DIRECTORY_MUSIC
// Environment.DIRECTORY_PODCASTS
// Environment.DIRECTORY_RINGTONES
// Environment.DIRECTORY_ALARMS
// Environment.DIRECTORY_NOTIFICATIONS
// Environment.DIRECTORY_PICTURES
// Environment.DIRECTORY_MOVIES
// Environment.DIRECTORY_DOWNLOADS
// Environment.DIRECTORY_DCIM
// /storage/emulated/0
File filePath1 = Environment.getExternalStoragePublicDirectory("");
// /storage/emulated/0/Download
File filePath2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// /storage/emulated/0/sharp
File filePath3 = Environment.getExternalStoragePublicDirectory("sharp");
5)getRootDirectory
示例
// /system
File filePath = Environment.getRootDirectory();
6)getExternalStorageState
示例
// return:
// Environment.MEDIA_UNKNOWN
// Environment.MEDIA_REMOVED
// Environment.MEDIA_UNMOUNTED
// Environment.MEDIA_CHECKING
// Environment.MEDIA_NOFS
// Environment.MEDIA_MOUNTED
// Environment.MEDIA_MOUNTED_READ_ONLY
// Environment.MEDIA_SHARED
// Environment.MEDIA_BAD_REMOVAL
// Environment.MEDIA_UNMOUNTABLE
String state = Environment.getExternalStorageState();
注意
- 当应用安装后,系统自动会创建
内部存储/Android/应用包名
目录,默认这个以包名命名的目录下会创建一个files
目录。当应用卸载时,这个以包名命名的目录也会被删除。
-
/sdcard
== Environment.getExternalStorageDirectory( )
== Environment.getExternalStoragePublicDirectory("")