鸟语天空
Android获取各种存储目录
post by:追风剑情 2016-8-22 11:02

示例代码

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.fragment_main);
	
	//Android 2.2(API level 8)之前用Environment类获取目录
	String ext_state = Environment.getExternalStorageState();
	File root_file = Environment.getRootDirectory();
	File data_file = Environment.getDataDirectory();
	File down_file = Environment.getDownloadCacheDirectory();
	File ext_file = Environment.getExternalStorageDirectory();
	File ext_pfile = Environment.getExternalStoragePublicDirectory("mydir");
	
	Log.d("test", "Environment.getExternalStorageState: " + ext_state);
	Log.d("test", "Environment.getRootDirectory: " + root_file.getAbsolutePath());
	Log.d("test", "Environment.getDataDirectory: " + data_file.getAbsolutePath());
	Log.d("test", "Environment.getDownloadCacheDirectory: " + down_file.getAbsolutePath());
	Log.d("test", "Environment.getExternalStorageDirectory: " + ext_file.getAbsolutePath());
	Log.d("test", "Environment.getExternalStoragePublicDirectory: " + ext_pfile.getAbsolutePath());
	
	//Android 2.2(API level 8)之后可从应用上下文中获取目录
	
	//Context.MODE_PRIVATE 默认操作模式,表示私有数据,只能被创建它的应用访问。
	//Context.MODE_WORLD_READABLE 可被其他应用读取
	//Context.MODE_WORLD_WRITEABLE 可被其他应用写入
	//Context.MODE_APPEND 文件不存在,则创建,若存在,则追加写入
	
	File dir = this.getDir("mydir", Context.MODE_PRIVATE);
	File fileDir = this.getFilesDir();
	File extDir = this.getExternalFilesDir("mydir");
	File cacheDir = this.getCacheDir();
	File extCacheDir = this.getExternalCacheDir();
	File databasePath = this.getDatabasePath("mydir");
	File streamPath = this.getFileStreamPath("mydir");
	File obbDir = this.getObbDir();
	
	Log.d("test", "this.getDir: " + dir.getAbsolutePath());
	Log.d("test", "this.getFilesDir: " + fileDir.getAbsolutePath());
	Log.d("test", "this.getExternalFilesDir: " + extDir.getAbsolutePath());
	Log.d("test", "this.getCacheDir: " + cacheDir.getAbsolutePath());
	Log.d("test", "this.getExternalCacheDir: " + extCacheDir.getAbsolutePath());
	Log.d("test", "this.getDatabasePath: " + databasePath.getAbsolutePath());
	Log.d("test", "this.getFileStreamPath: " + streamPath.getAbsolutePath());
	Log.d("test", "this.getObbDir: " + obbDir.getAbsolutePath());
}

 

Logcat日志

111111.png

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容