注意:由于Android6.0+支持多用戶搂抒,所以扮惦,在4.1.2 返回路徑
/data/data/com.xxx.xxx/
請(qǐng)自動(dòng)看做 6.0 下的/data/user/0/com.xxx.xxx/
目錄
每次都記不住Android中獲取的文件路徑具體是哪個(gè)臀蛛,整理了一下常用的路徑獲取方式。以下代碼運(yùn)行在Activity的onCreate方法中崖蜜,假設(shè)當(dāng)前項(xiàng)目的包名為me.febsky.debug
浊仆,記得請(qǐng)求讀寫sd卡權(quán)限,6.0要?jiǎng)討B(tài)獲取
String[] permissions = {"android.permission.WRITE_EXTERNAL_STORAGE"};
ActivityCompat.requestPermissions(this, permissions, 1); // without sdk version check
AndroidManifest.xml
中添加如下代碼
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
測(cè)試代碼
//Environment.getDataDirectory()--->/data
Log.d("Q_M", "Environment.getDataDirectory()--->" + Environment.getDataDirectory());
//Environment.getDownloadCacheDirectory()--->/cache
Log.d("Q_M", "Environment.getDownloadCacheDirectory()--->" + Environment.getDownloadCacheDirectory());
//Environment.getExternalStorageDirectory()--->/storage/emulated/0
Log.d("Q_M", "Environment.getExternalStorageDirectory()--->" + Environment.getExternalStorageDirectory());
// Environment.getExternalStoragePublicDirectory--->/storage/emulated/0/Music
Log.d("Q_M", "Environment.getExternalStoragePublicDirectory--->" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC));
//Environment.getRootDirectory()--->/system
Log.d("Q_M", "Environment.getRootDirectory()--->" + Environment.getRootDirectory());
//getApplication().getExternalCacheDir()--->/storage/emulated/0/Android/data/me.febsky.debug/cache
Log.d("Q_M", "getApplication().getExternalCacheDir()--->" + getApplication().getExternalCacheDir());
//getApplication().getExternalFilesDir--->/storage/emulated/0/Android/data/me.febsky.debug/files/Music
Log.d("Q_M", "getApplication().getExternalFilesDir--->" + getApplication().getExternalFilesDir(android.os.Environment.DIRECTORY_MUSIC));
//getApplication().getCacheDir()--->/data/data/me.febsky.debug/cache
Log.d("Q_M", "getApplication().getCacheDir()--->" + getApplication().getCacheDir());
//getApplication().getFilesDir()--->/data/data/me.febsky.debug/files
Log.d("Q_M", "getApplication().getFilesDir()--->" + getApplication().getFilesDir());
//下面這個(gè)方法受限于api 24
//Log.d("Q_M", " getApplication().getDataDir()--->" + getApplication().getDataDir());
//getApplication().getFileStreamPath--->/data/data/me.febsky.debug/files/yourfile
Log.d("Q_M", "getApplication().getFileStreamPath--->" + getApplication().getFileStreamPath("yourfile"));
//getApplication().getObbDir()--->/storage/emulated/0/Android/obb/me.febsky.debug
Log.d("Q_M", "getApplication().getObbDir()--->" + getApplication().getObbDir());
//getApplicationInfo().dataDir--->/data/data/me.febsky.debug
Log.d("Q_M", "getApplicationInfo().dataDir--->" + getApplicationInfo().dataDir);
//getApplicationInfo().publicSourceDir--->/data/app/me.febsky.debug-1/base.apk
Log.d("Q_M", "getApplicationInfo().publicSourceDir--->" + getApplicationInfo().publicSourceDir);
//getApplicationInfo().sourceDir--->/data/app/me.febsky.debug-1/base.apk
Log.d("Q_M", "getApplicationInfo().sourceDir--->" + getApplicationInfo().sourceDir);
///data/data/me.febsky.debug/app_yourfile 執(zhí)行這行代碼會(huì) 創(chuàng)建名為app_yourfile 的文件夾
Log.d("Q_M", "getBaseContext().getDir--->" + getBaseContext().getDir("yourfile", MODE_PRIVATE));
//下面省略和getApplication 中相同的方法豫领,Application也是間接繼承自Context類
路徑的規(guī)律圖如下
一般地抡柿,通過Context
和 Environment
相關(guān)的方法獲取文件存取的路徑。
通過這兩個(gè)類可獲取各種路徑等恐,如圖:
+- /data ---> Environment.getDataDirectory()
| |
| |
| +- data/com.adorkable.sample
| |
| |
| +- files ---> context.getFilesDir()
| | |
| | +- file1 ---> context.getFileStreamPath("file1")
| | |
| | +- file2 --->context.openFileOutput("file2")
| |
| +- cache ---> context.getCacheDir()
| |
| +- app_$name ---> context.getDir(String name, int mode)
| |
| +- shared_prefs ---> context.getSharedPreferences()
|
|
+- /storage/sdcard0 ---> Environment.getExternalStorageDirectory()
|
|- Music --->Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
|
+- dir1 --->Environment.getExternalStoragePublicDirectory("dir1")
|
+- Andorid/data/com.adorkable.sample【注:這里是包名】
|
|
+- files ---> context.getExternalFilesDir("")
| |
| +- file1 ---> context.getExternalFilesDir("file1")
| |
| +- Music ---> context.getExternalFilesDir(Environment.Music)
| |
| +- Picture ---> context.getExternalFilesDir(Environment.Picture)
| |
| +- ...
|
|
+- cache --->context.getExternalCacheDir()
|
+- 其他
```
【參考】
[http://febsky.me](http://febsky.me/2015/07/14/2015-07-14-Android%20%E6%96%87%E4%BB%B6%E5%AD%98%E5%82%A8/)