標籤:
一般的,我們可以通過context和Environment來擷取要儲存檔案的目錄
($rootDir)+- /data -> Environment.getDataDirectory()| || | ($appDataDir)| +- data/com.srain.cube.sample| || | ($filesDir)| +- files -> Context.getFilesDir() / Context.getFileStreamPath("")| | || | +- file1 -> Context.getFileStreamPath("file1")| | ($cacheDir)| +- cache -> Context.getCacheDir()| || +- app_$name ->(Context.getDir(String name, int mode)|($rootDir)+- /storage/sdcard0 -> Environment.getExternalStorageDirectory() | / Environment.getExternalStoragePublicDirectory("") | +- dir1 -> Environment.getExternalStoragePublicDirectory("dir1") | | ($appDataDir) +- Andorid/data/com.srain.cube.sample | | ($filesDir) +- files -> Context.getExternalFilesDir("") | | | +- file1 -> Context.getExternalFilesDir("file1") | +- Music -> Context.getExternalFilesDir(Environment.Music); | +- Picture -> ... Environment.Picture | +- ... | | ($cacheDir) +- cache -> Context.getExternalCacheDir() | +- ???各個路徑的特性
下面介紹這些路徑的特性以及使用中需要注意的細節:
1.根目錄($rootDir):
樣本
Environment.getDataDirectory(): /dataEnvironment.getExternalStorageDirectory(): /storage/sdcard0
2.應用資料目錄($appDataDir)
- 內部儲存:
$appDataDir = $rootDir/data/$packageName,
- 外部儲存:
$appDataDir = $rootDir/Andorid/data/$packageName
在這些目錄下的資料,在app卸載之後,會被系統刪除,我們應將應用的資料放於這兩個目錄中。
3.外部儲存中,公開的資料目錄。 這些目錄將不會隨著應用的刪除而被系統刪除,請斟酌使用:
Environment.getExternalStorageDirectory(): /storage/sdcard0// 同 $rootDirEnvironment.getExternalStoragePublicDirectory(""): /storage/sdcard0Environment.getExternalStoragePublicDirectory("folder1"): /storage/sdcard0/folder1
4.應用資料目錄下的目錄
一般的在$appDataDir下,會有兩個目錄:
1. 資料緩衝:$cacheDir = $appDataDir/cache:
Context.getCacheDir(): /data/data/com.srain.cube.sample/cache Context.getExternalCacheDir(): /storage/sdcard0/Android/data/com.srain.cube.sample/cache
2.檔案目錄 $filesDir = $appDataDir/files:
內部儲存:通過Context.getFilesDir() 擷取
Context.getFileStreamPath(String name)返回以name為檔案名稱的檔案對象,name為空白,則返回 $filesDir 本身
樣本:
Context.getFilesDir(): /data/data/com.srain.cube.sample/filesContext.getFileStreamPath(""): /data/data/com.srain.cube.sample/filesContext.getFileStreamPath("file1"): /data/data/com.srain.cube.sample/files/file1
外部儲存:通過Context.getExternalFilesDir(String type), type為空白字串時擷取.
type系統指定了幾種類型:
Environment.DIRECTORY_MUSIC Environment.DIRECTORY_PICTURES ...
樣本
Context.getExternalFilesDir(""): /storage/sdcard0/Android/data/com.srain.cube.sample/files Context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) /storage/sdcard0/Android/data/com.srain.cube.sample/files/Music
3.$cacheDir / $filesDir 安全性
在內部儲存中,$cacheDir, $filesDir是app安全的,其他應用無法讀取本應用的資料,而外部儲存則不是。
在外部儲存中,這兩個檔案夾其他應用程式也可訪問。
在外部儲存中,$filesDir中的媒體檔案,不會被當做媒體掃描出來,加到媒體庫中。
4.$cacheDir / $filesDir 同級目錄
在內部儲存中:通過 Context.getDir(String name, int mode)可擷取和 $filesDir / $cacheDir 同級的目錄
目錄的命名規則為 app_ + name, 通過mode可控制此目錄為app私人還是其他app可讀寫。
樣本:
Context.getDir("dir1", MODE_PRIVATE): Context.getDir: /data/data/com.srain.cube.sample/app_dir1
5.特別注意, 對於外部儲存,擷取$cacheDir 或者 $filesDir及其下的路徑
在API level 8 以下,或者空間不足,相關的方法獲路徑為空白時,需要自己構造。
@TargetApi(VERSION_CODES.FROYO)public static File getExternalCacheDir(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)) { File path = context.getExternalCacheDir(); // In some case, even the sd card is mounted, // getExternalCacheDir will return null // may be it is nearly full. if (path != null) { return path; } } // Before Froyo or the path is null, // we need to construct the external cache folder ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);}
參考:http://liaohuqiu.net/cn/posts/storage-in-android/
android 儲存檔案的各種目錄列表