欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android获取存储路径

程序员文章站 2022-07-15 09:59:12
...
        //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());
        //下面这个方法受限于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  获取当前应用的Apk的存放目录代码:
        Log.d("Q_M", "getApplicationInfo().sourceDir--->" + getApplicationInfo().sourceDir);

        ///data/data/me.febsky.debug/app_yourfile 执行这行代码会 创建名为app_yourfile 的文件夹
        Log.d("Q_M", "getBaseContext().getDir--->" + getBaseContext().getDir("yourfile", MODE_PRIVATE));
        //下面省略和getApplication 中相同的方法,Application也是间接继承自Context类

路径的规律图如下

一般地,通过Context 和 Environment相关的方法获取文件存取的路径。

通过这两个类可获取各种路径,如图:

    +- /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/)
相关标签: 存储