【android】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

测试案例注意:

利用

this.getFilesDir()
仅可以获取到/data/data/com.example.client/files                目录,需要用这种方法直接删除指定文件时,需要注意加”/123.txt

结论:

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

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

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值