Android下获取各种存储目录

直接上代码:

[java]
File dateDir = Environment.getDataDirectory(); 
String dirName = dateDir.getAbsolutePath(); //result is: /data  
dateDir = Environment.getExternalStorageDirectory(); 
StringBuilder sbBuilder = new StringBuilder(); 
sbBuilder.append("The sdcard mounted?" + Environment.MEDIA_MOUNTED +"\n"); 
sbBuilder.append("GetDatedirectory:"+dirName+"\n"); 
sbBuilder.append("Environment.getExternalStorageDirectory:"+dateDir.getAbsolutePath()+"\n");         
sbBuilder.append("context.getExternalFilesDir:"+this.getExternalFilesDir(null)+"\n"); 
sbBuilder.append("context.getFiledir:"+this.getFilesDir().getAbsolutePath()+"\n"); 
sbBuilder.append("context.getCachedir:"+this.getCacheDir().getAbsolutePath()+"\n"); 
TextView textView = new TextView(this); 
textView.setText(sbBuilder.toString()); 

File dateDir = Environment.getDataDirectory();
String dirName = dateDir.getAbsolutePath(); //result is: /data
dateDir = Environment.getExternalStorageDirectory();
StringBuilder sbBuilder = new StringBuilder();
sbBuilder.append("The sdcard mounted?" + Environment.MEDIA_MOUNTED +"\n");
sbBuilder.append("GetDatedirectory:"+dirName+"\n");
sbBuilder.append("Environment.getExternalStorageDirectory:"+dateDir.getAbsolutePath()+"\n");  
sbBuilder.append("context.getExternalFilesDir:"+this.getExternalFilesDir(null)+"\n");
sbBuilder.append("context.getFiledir:"+this.getFilesDir().getAbsolutePath()+"\n");
sbBuilder.append("context.getCachedir:"+this.getCacheDir().getAbsolutePath()+"\n");
TextView textView = new TextView(this);
textView.setText(sbBuilder.toString());

输出结果:

 

[html]
04-16 01:27:02.255: I/log(1892): The sdcard mounted?mounted 
 
04-16 01:27:02.255: I/log(1892): GetDatedirectory:/data 
 
04-16 01:27:02.255: I/log(1892): Environment.getExternalStorageDirectory:/mnt/sdcard 
 
04-16 01:27:02.255: I/log(1892): context.getExternalFilesDir:/mnt/sdcard/Android/data/com.example.client/files 
 
04-16 01:27:02.255: I/log(1892): context.getFiledir:/data/data/com.example.client/files 
 
04-16 01:27:02.255: I/log(1892): context.getCachedir:/data/data/com.example.client/cache 

04-16 01:27:02.255: I/log(1892): The sdcard mounted?mounted

04-16 01:27:02.255: I/log(1892): GetDatedirectory:/data

04-16 01:27:02.255: I/log(1892): Environment.getExternalStorageDirectory:/mnt/sdcard

04-16 01:27:02.255: I/log(1892): context.getExternalFilesDir:/mnt/sdcard/Android/data/com.example.client/files

04-16 01:27:02.255: I/log(1892): context.getFiledir:/data/data/com.example.client/files

04-16 01:27:02.255: I/log(1892): context.getCachedir:/data/data/com.example.client/cache

结论:

1.使用Environment.getExternalStorageDirectory可以得到系统的sdcard路径,不过这个一般在各个手机上都是一样的。

2.使用context.getExternalFilesDir可以得到系统为程序在sdcard上分配的存储路径,据说放在这里卸载程序时目录也会被删除;

3.使用context.getFileDir可以获得程序的data目录的files子目录,如果有小文件,sdcard又不存在时可以选择放这里。



我会持续不断的为大家奉送干货 , 另外小店刚开业 求光顾啊  你懂得~ 

http://shop117097708.taobao.com/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值