直接上代码:
[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又不存在时可以选择放这里。
[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又不存在时可以选择放这里。