androd下各种文件目录获取

2021-06-28  本文已影响0人  蘇三子
/**
     * context.getObbDir()  /storage/emulated/0/Android/obb/{当前包名}
     * context.getObbDirs[0]    /storage/emulated/0/Android/obb/{当前包名}
     * context.getDataDir() /data/user/0/{当前包名}
     * context.getCacheDir()    /data/user/0/{当前包名}/cache
     * context.getFilesDir()    /data/user/0/{当前包名}/files
     * context.getCodeCacheDir()    /data/user/0/{当前包名}/code_cache
     * context.getExternalCacheDir()    /storage/emulated/0/Android/data/{当前包名}/cache
     * context.getExternalCacheDirs[0]  /storage/emulated/0/Android/data/{当前包名}/cache
     * context.getExternalFilesDir(null)    /storage/emulated/0/Android/data/{当前包名}/files
     * context.getExternalFilesDir(DIRECTORY_DCIM)  /storage/emulated/0/Android/data/{当前包名}/files/DCIM
     * context.getExternalFilesDir(DIRECTORY_MUSIC) /storage/emulated/0/Android/data/{当前包名}/files/Music
     * context.getExternalFilesDir(DIRECTORY_ALARMS)    /storage/emulated/0/Android/data/{当前包名}/files/Alarms
     * context.getExternalFilesDir(DIRECTORY_MOVIES)    /storage/emulated/0/Android/data/{当前包名}/files/Movies
     * context.getExternalFilesDir(DIRECTORY_PICTURES)  /storage/emulated/0/Android/data/{当前包名}/files/Pictures
     * context.getExternalFilesDir(DIRECTORY_PODCASTS)  /storage/emulated/0/Android/data/{当前包名}/files/Podcasts
     * context.getExternalFilesDir(DIRECTORY_DOCUMENTS) /storage/emulated/0/Android/data/{当前包名}/files/Documents
     * context.getExternalFilesDir(DIRECTORY_DOWNLOADS) /storage/emulated/0/Android/data/{当前包名}/files/Download
     * context.getExternalFilesDir(DIRECTORY_RINGTONES) /storage/emulated/0/Android/data/{当前包名}/files/Ringtones
     * context.getExternalFilesDir(DIRECTORY_NOTIFICATIONS) /storage/emulated/0/Android/data/{当前包名}/files/Notifications
     * context.getExternalMediaDirs[0]  /storage/emulated/0/Android/media/{当前包名}
     * Environment.getDataDirectory()   /data
     * Environment.getRootDirectory()   /system
     * Environment.getDownloadCacheDirectory()  /data/cache
     * Environment.getExternalStorageDirectory()    /storage/emulated/0
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_DCIM)    /storage/emulated/0/DCIM
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC)   /storage/emulated/0/Music
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_ALARMS)  /storage/emulated/0/Alarms
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES)  /storage/emulated/0/Movies
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES)    /storage/emulated/0/Pictures
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_PODCASTS)    /storage/emulated/0/Podcasts
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS)   /storage/emulated/0/Documents
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)   /storage/emulated/0/Download
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_RINGTONES)   /storage/emulated/0/Ringtones
     * Environment.getExternalStoragePublicDirectory(DIRECTORY_NOTIFICATIONS)   /storage/emulated/0/Notifications
     */
    public static String test(Context context) {
        StringBuilder sb = new StringBuilder("各方法输出目录如下:\n");
        sb.append("context.getObbDir()\t").append(context.getObbDir()).append('\n');

        File[] files = context.getObbDirs();
        for (int i = 0, size = files != null ? files.length : 0; i < size; i++) {
            sb.append("context.getObbDirs[").append(i).append("]\t").append(files[i]).append('\n');
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sb.append("context.getDataDir()\t").append(context.getDataDir()).append('\n');
        }
        sb.append("context.getCacheDir()\t").append(context.getCacheDir()).append('\n');
        sb.append("context.getFilesDir()\t").append(context.getFilesDir()).append('\n');
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            sb.append("context.getCodeCacheDir()\t").append(context.getCodeCacheDir()).append('\n');
        }
        sb.append("context.getExternalCacheDir()\t").append(context.getExternalCacheDir()).append('\n');

        files = context.getExternalCacheDirs();
        for (int i = 0, size = files != null ? files.length : 0; i < size; i++) {
            sb.append("context.getExternalCacheDirs[").append(i).append("]\t").append(files[i]).append('\n');
        }
        sb.append("context.getExternalFilesDir(null)\t").append(context.getExternalFilesDir(null)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_DCIM)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_DCIM)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_MUSIC)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_ALARMS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_MOVIES)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_MOVIES)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_PICTURES)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_PODCASTS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_PODCASTS)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_DOCUMENTS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_DOWNLOADS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)).append('\n');
        sb.append("context.getExternalFilesDir(DIRECTORY_RINGTONES)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES)).append('\n');
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            sb.append("context.getExternalFilesDir(DIRECTORY_AUDIOBOOKS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_AUDIOBOOKS)).append('\n');
            sb.append("context.getExternalFilesDir(DIRECTORY_SCREENSHOTS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_SCREENSHOTS)).append('\n');
        }
        sb.append("context.getExternalFilesDir(DIRECTORY_NOTIFICATIONS)\t").append(context.getExternalFilesDir(Environment.DIRECTORY_NOTIFICATIONS)).append('\n');

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            files = context.getExternalMediaDirs();
            for (int i = 0, size = files != null ? files.length : 0; i < size; i++) {
                sb.append("context.getExternalMediaDirs[").append(i).append("]\t").append(files[i]).append('\n');
            }
        }

        sb.append("Environment.getDataDirectory()\t").append(Environment.getDataDirectory()).append('\n');
        sb.append("Environment.getRootDirectory()\t").append(Environment.getRootDirectory()).append('\n');
        sb.append("Environment.getDownloadCacheDirectory()\t").append(Environment.getDownloadCacheDirectory()).append('\n');
        sb.append("Environment.getExternalStorageDirectory()\t").append(Environment.getExternalStorageDirectory()).append('\n');

        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_DCIM)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_ALARMS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_PODCASTS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)).append('\n');
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_RINGTONES)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)).append('\n');
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_AUDIOBOOKS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_AUDIOBOOKS)).append('\n');
            sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_SCREENSHOTS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_SCREENSHOTS)).append('\n');
        }
        sb.append("Environment.getExternalStoragePublicDirectory(DIRECTORY_NOTIFICATIONS)\t").append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS)).append('\n');

        sb.append("Environment.getExternalStorageState()\t").append(Environment.getExternalStorageState()).append('\n');
        return sb.toString();
    }
上一篇下一篇

猜你喜欢

热点阅读