Android获取文件夹api及对应存储地址整理

本文整理了一些安卓中常用的获取文件夹的api, 并得到它们的实际存储地址, 方便开发者了解在做文件存储时,文件会具体存放的位置所在。

代码运行环境为华为P9 Android 8.0, 项目版本配置如下:

compileSdkVersion 28
minSdkVersion 22
targetSdkVersion 28

不同版本手机,打印出来的地址可能会有出入, 建议可以复制如下代码进行实际测试:

kotlin版本

 private fun printFileDir() {
        var paths: StringBuilder = java.lang.StringBuilder()
        paths.append("\n------------------------------------------------------------------------------- \n")
        paths.append("\n获取目录api对应的存储地址如下: \n")
        paths.append("\nEnvironment.getDataDirectory() :\t" + Environment.getDataDirectory())
        paths.append("\nEnvironment.getDownloadCacheDirectory() :\t" + Environment.getDownloadCacheDirectory())
        paths.append("\nEnvironment.getExternalStorageDirectory() :\t" + Environment.getExternalStorageDirectory())
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_ALARMS
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MUSIC
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_NOTIFICATIONS
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PODCASTS
            )
        )
        paths.append(
            "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) :\t" + Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_RINGTONES
            )
        )
        paths.append("\nEnvironment.getExternalStorageState() :\t" + Environment.getExternalStorageState())
        paths.append("\nEnvironment.getRootDirectory() :\t" + Environment.getRootDirectory())
        paths.append("\nContextCompat.getExternalCacheDirs(context!!) :\t" + ContextCompat.getExternalCacheDirs(context!!).get(0).path)
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_ALARMS) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_ALARMS
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DCIM) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_DCIM
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOCUMENTS) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_DOCUMENTS
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOWNLOADS) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_DOWNLOADS
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MOVIES) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_MOVIES
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MUSIC) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_MUSIC
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_NOTIFICATIONS) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_NOTIFICATIONS
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PICTURES) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_PICTURES
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PODCASTS) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_PODCASTS
            ).get(0).path
        )
        paths.append(
            "\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_RINGTONES) :\t" + ContextCompat.getExternalFilesDirs(
                context!!,
                Environment.DIRECTORY_RINGTONES
            ).get(0).path
        )

        paths.append("\n------------------------------------------------------------------------------- \n")
        paths.append("\n以上为常用目录打印结果\n")
        Log.d("file_operator", paths.toString())
    }

注意,ContextCompat.getExternalFilesDirs()是为Android 4.3及以下版本为获取外置存储目录的兼容方法,返回的是一个文件数组列表。如果你的应用兼容到4.3及以下,在选择外置存储时,请默认选择第一个文件路径,只有当它控件满了或者不可用时,在选择其它可用的外置存储。

Java版本

private void printFileDir(Context context) {
        StringBuilder paths  = new StringBuilder();
        paths.append("\n------------------------------------------------------------------------------- \n");
        paths.append("\n获取目录api对应的存储地址如下: \n");
        paths.append("\nEnvironment.getDataDirectory() :\t" + Environment.getDataDirectory());
        paths.append("\nEnvironment.getDownloadCacheDirectory() :\t" + Environment.getDownloadCacheDirectory());
        paths.append("\nEnvironment.getExternalStorageDirectory() :\t" + Environment.getExternalStorageDirectory());
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_ALARMS
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DCIM
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOCUMENTS
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOWNLOADS
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_MOVIES
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_MUSIC
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_NOTIFICATIONS
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PODCASTS
                )
        );
        paths.append(
                "\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) :\t" + Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_RINGTONES
                )
        );
        paths.append("\nEnvironment.getExternalStorageState() :\t" + Environment.getExternalStorageState());
        paths.append("\nEnvironment.getRootDirectory() :\t" + Environment.getRootDirectory());
        paths.append("\nContextCompat.getExternalCacheDirs(context) :\t" + ContextCompat.getExternalCacheDirs(context));
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_ALARMS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_ALARMS
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DCIM)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_DCIM
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOCUMENTS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_DOCUMENTS
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_DOWNLOADS
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_MOVIES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_MOVIES
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_MUSIC)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_MUSIC
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_NOTIFICATIONS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_NOTIFICATIONS
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_PICTURES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_PICTURES
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_PODCASTS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                        context,
                Environment.DIRECTORY_PODCASTS
            )[0].getPath()
        );
        paths.append(
                "\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_RINGTONES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
                       context,
                Environment.DIRECTORY_RINGTONES
            )[0].getPath()
        );

        paths.append("\n------------------------------------------------------------------------------- \n");
        paths.append("\n以上为常用目录打印结果\n");
        Log.d("file_operator", paths.toString());
    }

打印的结果如下:

    ------------------------------------------------------------------------------- 
    
    获取目录api对应的存储地址如下: 
    
    Environment.getDataDirectory() :    /data
    Environment.getDownloadCacheDirectory() :   /data/cache
    Environment.getExternalStorageDirectory() : /storage/emulated/0
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) :   /storage/emulated/0/Alarms
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) : /storage/emulated/0/DCIM
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) :    /storage/emulated/0/Documents
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) :    /storage/emulated/0/Download
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) :   /storage/emulated/0/Movies
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) :    /storage/emulated/0/Music
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) :    /storage/emulated/0/Notifications
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) : /storage/emulated/0/Pictures
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) : /storage/emulated/0/Podcasts
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) :    /storage/emulated/0/Ringtones
    Environment.getExternalStorageState() : mounted
    Environment.getRootDirectory() :    /system
    ContextCompat.getExternalCacheDirs(context!!) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/cache
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_ALARMS) :   /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Alarms
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DCIM) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/DCIM
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOCUMENTS) :    /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Documents
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOWNLOADS) :    /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Download
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MOVIES) :   /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Movies
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MUSIC) :    /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Music
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_NOTIFICATIONS) :    /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Notifications
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PICTURES) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Pictures
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PODCASTS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Podcasts
    ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_RINGTONES) :    /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Ringtones
    ------------------------------------------------------------------------------- 
    
    以上为常用目录打印结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343