Android下獲取各種存儲目錄


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又不存在時可以選擇放這裡。

發佈留言