美文网首页Android知识Android开发经验谈Android开发
Android中的文件路径都指向了哪里

Android中的文件路径都指向了哪里

作者: 千山万水迷了鹿 | 来源:发表于2017-07-11 18:16 被阅读672次

    注意:由于Android6.0+支持多用户,所以,在4.1.2 返回路径 /data/data/com.xxx.xxx/
    请自动看做 6.0 下的/data/user/0/com.xxx.xxx/目录

    每次都记不住Android中获取的文件路径具体是哪个,整理了一下常用的路径获取方式。以下代码运行在Activity的onCreate方法中,假设当前项目的包名为me.febsky.debug,记得请求读写sd卡权限,6.0要动态获取

    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"/>
    

    测试代码

            //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
            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类
    

    路径的规律图如下

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

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

        +- /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/)

    相关文章

      网友评论

        本文标题:Android中的文件路径都指向了哪里

        本文链接:https://www.haomeiwen.com/subject/edbmhxtx.html