|
| 1 | +package com.blankj.subutil.util; |
| 2 | + |
| 3 | +import android.os.Build; |
| 4 | +import android.os.Environment; |
| 5 | +import android.support.annotation.RequiresApi; |
| 6 | + |
| 7 | +/** |
| 8 | + * <pre> |
| 9 | + * author: Blankj |
| 10 | + * blog : http://blankj.com |
| 11 | + * time : 2018/04/15 |
| 12 | + * desc : 路径相关工具类 |
| 13 | + * </pre> |
| 14 | + */ |
| 15 | +public class PathUtils { |
| 16 | + |
| 17 | + private PathUtils() { |
| 18 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * 获取 Android 系统根目录 |
| 23 | + * <pre>path: /system</pre> |
| 24 | + * |
| 25 | + * @return 系统根目录 |
| 26 | + */ |
| 27 | + public static String getRootPath() { |
| 28 | + return Environment.getRootDirectory().getAbsolutePath(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 获取 data 目录 |
| 33 | + * <pre>path: /data</pre> |
| 34 | + * |
| 35 | + * @return data 目录 |
| 36 | + */ |
| 37 | + public static String getDataPath() { |
| 38 | + return Environment.getDataDirectory().getAbsolutePath(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * 获取缓存目录 |
| 43 | + * <pre>path: data/cache</pre> |
| 44 | + * |
| 45 | + * @return 缓存目录 |
| 46 | + */ |
| 47 | + public static String getIntDownloadCachePath() { |
| 48 | + return Environment.getDownloadCacheDirectory().getAbsolutePath(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 获取此应用的缓存目录 |
| 53 | + * <pre>path: /data/data/package/cache</pre> |
| 54 | + * |
| 55 | + * @return 此应用的缓存目录 |
| 56 | + */ |
| 57 | + public static String getAppIntCachePath() { |
| 58 | + return Utils.getApp().getCacheDir().getAbsolutePath(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 获取此应用的文件目录 |
| 63 | + * <pre>path: /data/data/package/files</pre> |
| 64 | + * |
| 65 | + * @return 此应用的文件目录 |
| 66 | + */ |
| 67 | + public static String getAppIntFilesPath() { |
| 68 | + return Utils.getApp().getFilesDir().getAbsolutePath(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 获取此应用的数据库文件目录 |
| 73 | + * <pre>path: /data/data/package/databases/name</pre> |
| 74 | + * |
| 75 | + * @param name 数据库文件名 |
| 76 | + * @return 数据库文件目录 |
| 77 | + */ |
| 78 | + public static String getAppIntDbPath(String name) { |
| 79 | + return Utils.getApp().getDatabasePath(name).getAbsolutePath(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 获取 Android 外置储存的根目录 |
| 84 | + * <pre>path: /storage/emulated/0</pre> |
| 85 | + * |
| 86 | + * @return 外置储存根目录 |
| 87 | + */ |
| 88 | + public static String getExtStoragePath() { |
| 89 | + return Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 获取闹钟铃声目录 |
| 94 | + * <pre>path: /storage/emulated/0/Alarms</pre> |
| 95 | + * |
| 96 | + * @return 闹钟铃声目录 |
| 97 | + */ |
| 98 | + public static String getExtAlarmsPath() { |
| 99 | + return Environment |
| 100 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) |
| 101 | + .getAbsolutePath(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * 获取相机拍摄的照片和视频的目录 |
| 106 | + * <pre>path: /storage/emulated/0/DCIM</pre> |
| 107 | + * |
| 108 | + * @return 照片和视频目录 |
| 109 | + */ |
| 110 | + public static String getExtDcimPath() { |
| 111 | + return Environment |
| 112 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) |
| 113 | + .getAbsolutePath(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * 获取文档目录 |
| 118 | + * <pre>path: /storage/emulated/0/Documents</pre> |
| 119 | + * |
| 120 | + * @return 文档目录 |
| 121 | + */ |
| 122 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) |
| 123 | + public static String getExtDocumentsPath() { |
| 124 | + return Environment |
| 125 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) |
| 126 | + .getAbsolutePath(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 获取下载目录 |
| 131 | + * <pre>path: /storage/emulated/0/Download</pre> |
| 132 | + * |
| 133 | + * @return 下载目录 |
| 134 | + */ |
| 135 | + public static String getExtDownloadsPath() { |
| 136 | + return Environment |
| 137 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
| 138 | + .getAbsolutePath(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 获取视频目录 |
| 143 | + * <pre>path: /storage/emulated/0/Movies</pre> |
| 144 | + * |
| 145 | + * @return 视频目录 |
| 146 | + */ |
| 147 | + public static String getExtMoviesPath() { |
| 148 | + return Environment |
| 149 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) |
| 150 | + .getAbsolutePath(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * 获取音乐目录 |
| 155 | + * <pre>path: /storage/emulated/0/Music</pre> |
| 156 | + * |
| 157 | + * @return 音乐目录 |
| 158 | + */ |
| 159 | + public static String getExtMusicPath() { |
| 160 | + return Environment |
| 161 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) |
| 162 | + .getAbsolutePath(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * 获取提示音目录 |
| 167 | + * <pre>path: /storage/emulated/0/Notifications</pre> |
| 168 | + * |
| 169 | + * @return 提示音目录 |
| 170 | + */ |
| 171 | + public static String getExtNotificationsPath() { |
| 172 | + return Environment |
| 173 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) |
| 174 | + .getAbsolutePath(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 获取图片目录 |
| 179 | + * <pre>path: /storage/emulated/0/Pictures</pre> |
| 180 | + * |
| 181 | + * @return 图片目录 |
| 182 | + */ |
| 183 | + public static String getExtPicturesPath() { |
| 184 | + return Environment |
| 185 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) |
| 186 | + .getAbsolutePath(); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * 获取 Podcasts 目录 |
| 191 | + * <pre>path: /storage/emulated/0/Podcasts</pre> |
| 192 | + * |
| 193 | + * @return Podcasts 目录 |
| 194 | + */ |
| 195 | + public static String getExtPodcastsPath() { |
| 196 | + return Environment |
| 197 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) |
| 198 | + .getAbsolutePath(); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * 获取铃声目录 |
| 203 | + * <pre>path: /storage/emulated/0/Ringtones</pre> |
| 204 | + * |
| 205 | + * @return 下载目录 |
| 206 | + */ |
| 207 | + public static String getExtRingtonesPath() { |
| 208 | + return Environment |
| 209 | + .getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) |
| 210 | + .getAbsolutePath(); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * 获取此应用在外置储存中的缓存目录 |
| 215 | + * <pre>path: /storage/emulated/0/Android/data/package/cache</pre> |
| 216 | + * |
| 217 | + * @return 此应用在外置储存中的缓存目录 |
| 218 | + */ |
| 219 | + public static String getAppExtCachePath() { |
| 220 | + return Utils.getApp().getExternalCacheDir().getAbsolutePath(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * 获取此应用在外置储存中的文件目录 |
| 225 | + * <pre>path: /storage/emulated/0/Android/data/package/files</pre> |
| 226 | + * |
| 227 | + * @return 此应用在外置储存中的文件目录 |
| 228 | + */ |
| 229 | + public static String getAppExtFilePath() { |
| 230 | + return Utils.getApp().getExternalFilesDir(null).getAbsolutePath(); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * 获取此应用在外置储存中的闹钟铃声目录 |
| 235 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Alarms</pre> |
| 236 | + * |
| 237 | + * @return 此应用在外置储存中的闹钟铃声目录 |
| 238 | + */ |
| 239 | + public static String getAppExtAlarmsPath() { |
| 240 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_ALARMS) |
| 241 | + .getAbsolutePath(); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * 获取此应用在外置储存中的相机目录 |
| 246 | + * <pre>path: /storage/emulated/0/Android/data/package/files/DCIM</pre> |
| 247 | + * |
| 248 | + * @return 此应用在外置储存中的相机目录 |
| 249 | + */ |
| 250 | + public static String getAppExtDcimPath() { |
| 251 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_DCIM) |
| 252 | + .getAbsolutePath(); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * 获取此应用在外置储存中的文档目录 |
| 257 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Documents</pre> |
| 258 | + * |
| 259 | + * @return 此应用在外置储存中的文档目录 |
| 260 | + */ |
| 261 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) |
| 262 | + public static String getAppExtDocumentsPath() { |
| 263 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) |
| 264 | + .getAbsolutePath(); |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * 获取此应用在外置储存中的闹钟目录 |
| 269 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Download</pre> |
| 270 | + * |
| 271 | + * @return 此应用在外置储存中的闹钟目录 |
| 272 | + */ |
| 273 | + public static String getAppExtDownloadPath() { |
| 274 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) |
| 275 | + .getAbsolutePath(); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * 获取此应用在外置储存中的视频目录 |
| 280 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Movies</pre> |
| 281 | + * |
| 282 | + * @return 此应用在外置储存中的视频目录 |
| 283 | + */ |
| 284 | + public static String getAppExtMoviesPath() { |
| 285 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_MOVIES) |
| 286 | + .getAbsolutePath(); |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * 获取此应用在外置储存中的音乐目录 |
| 291 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Music</pre> |
| 292 | + * |
| 293 | + * @return 此应用在外置储存中的音乐目录 |
| 294 | + */ |
| 295 | + public static String getAppExtMusicPath() { |
| 296 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_MUSIC) |
| 297 | + .getAbsolutePath(); |
| 298 | + } |
| 299 | + |
| 300 | + /** |
| 301 | + * 获取此应用在外置储存中的提示音目录 |
| 302 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Notifications</pre> |
| 303 | + * |
| 304 | + * @return 此应用在外置储存中的提示音目录 |
| 305 | + */ |
| 306 | + public static String getAppExtNotificationsPath() { |
| 307 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_NOTIFICATIONS) |
| 308 | + .getAbsolutePath(); |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * 获取此应用在外置储存中的图片目录 |
| 313 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Pictures</pre> |
| 314 | + * |
| 315 | + * @return 此应用在外置储存中的图片目录 |
| 316 | + */ |
| 317 | + public static String getAppExtPicturesPath() { |
| 318 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_PICTURES) |
| 319 | + .getAbsolutePath(); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * 获取此应用在外置储存中的 Podcasts 目录 |
| 324 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Podcasts</pre> |
| 325 | + * |
| 326 | + * @return 此应用在外置储存中的 Podcasts 目录 |
| 327 | + */ |
| 328 | + public static String getAppExtPodcastsPath() { |
| 329 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_PODCASTS) |
| 330 | + .getAbsolutePath(); |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * 获取此应用在外置储存中的铃声目录 |
| 335 | + * <pre>path: /storage/emulated/0/Android/data/package/files/Ringtones</pre> |
| 336 | + * |
| 337 | + * @return 此应用在外置储存中的铃声目录 |
| 338 | + */ |
| 339 | + public static String getAppExtRingtonesPath() { |
| 340 | + return Utils.getApp().getExternalFilesDir(Environment.DIRECTORY_RINGTONES) |
| 341 | + .getAbsolutePath(); |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * 获取此应用的 Obb 目录 |
| 346 | + * <pre>path: /storage/emulated/0/Android/obb/package</pre> |
| 347 | + * <pre>一般用来存放游戏数据包</pre> |
| 348 | + * |
| 349 | + * @return 此应用的 Obb 目录 |
| 350 | + */ |
| 351 | + public static String getObbPath() { |
| 352 | + return Utils.getApp().getObbDir().getAbsolutePath(); |
| 353 | + } |
| 354 | +} |
0 commit comments