相信很多新手对于Android的一些系统默认路径不太了解,在这里以5.1的Nexus5为例来介绍一下,
Environment.getDataDirectory().getPath()=/data
Environment.getDownloadCacheDirectory().getPath()=/cache
Environment.getExternalStorageDirectory()=/storage/emulated/0
Environment.getRootDirectory().getPath()=/system
//警报的铃声
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS).getPath()=/storage/emulated/0/Alarms
//相机拍摄的图片和视频保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()=/storage/emulated/0/DCIM
//下载文件保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()=/storage/emulated/0/Download
//电影保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath()=/storage/emulated/0/Movies
//音乐保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()=/storage/emulated/0/Music
//通知音保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS).getPath()=/storage/emulated/0/Notifications
//下载的图片保存的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()=/storage/emulated/0/Pictures
//用于保存podcast(博客)的音频文件
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS).getPath()=/storage/emulated/0/Podcasts
//保存铃声的位置
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getPath()=/storage/emulated/0/Ringtones
context.getCacheDir().getPath()=/data/data/包名/cache
context.getFilesDir().getPath()=/data/data/包名/files
context.getObbDir().getPath()=/storage/emulated/0/Android/obb/包名
context.getExternalCacheDir().getPath()=/storage/emulated/0/Android/data/包名/cache
context.getExternalFilesDir(Environment.DIRECTORY_ALARMS).getPath()=/storage/emulated/0/Android/data/包名/files/Alarms
context.getExternalFilesDir(Environment.DIRECTORY_DCIM).getPath()=/storage/emulated/0/Android/data/包名/files/DCIM
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath()=/storage/emulated/0/Android/data/包名/files/Download
context.getExternalFilesDir(Environment.DIRECTORY_MOVIES).getPath()=/storage/emulated/0/Android/data/包名/files/Movies
context.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath()=/storage/emulated/0/Android/data/包名/files/Music
context.getExternalFilesDir(Environment.DIRECTORY_NOTIFICATIONS).getPath()=/storage/emulated/0/Android/data/包名/files/Notifications
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath()=/storage/emulated/0/Android/data/包名/files/Pictures
context.getExternalFilesDir(Environment.DIRECTORY_PODCASTS).getPath()=/storage/emulated/0/Android/data/包名/files/Podcasts
context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES).getPath()=/storage/emulated/0/Android/data/包名/files/Ringtones
context.getDatabasePath("XXX").getPath()=/data/data/包名/databases/XXX
context.getPackageCodePath()=/data/app/包名-2/base.apk
context.getPackageResourcePath()=/data/app/包名-2/base.apk
类:
package com.mufeng.toolproject.utils;
import android.content.Context;
import android.os.Environment;
/**
* Created by mufeng on 2017/3/11.
*/
public class DirectoryUtils {
private static final String TAG = "DirectoryUtils";
public static void getEnvironmentDirectories() {
//:/system
String rootDir = Environment.getRootDirectory().toString();
System.out.println("Environment.getRootDirectory()=:" + rootDir);
//:/data 用户数据目录
String dataDir = Environment.getDataDirectory().toString();
System.out.println("Environment.getDataDirectory()=:" + dataDir);
//:/cache 下载缓存内容目录
String cacheDir = Environment.getDownloadCacheDirectory().toString();
System.out.println("Environment.getDownloadCacheDirectory()=:" + cacheDir);
//:/mnt/sdcard或者/storage/emulated/0或者/storage/sdcard0 主要的外部存储目录
//这个不一定是外部存储
String storageDir = Environment.getExternalStorageDirectory().toString();
System.out.println("Environment.getExternalStorageDirectory()=:" + storageDir);
//:/mnt/sdcard/Pictures或者/storage/emulated/0/Pictures或者/storage/sdcard0/Pictures
String publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
System.out.println("Environment.getExternalStoragePublicDirectory()=:" + publicDir);
//获取SD卡是否存在:mounted
String storageState = Environment.getExternalStorageState().toLowerCase();
System.out.println("Environment.getExternalStorageState()=:" + storageState);
//设备的外存是否是用内存模拟的,是则返回true。(API Level 11)
boolean isEmulated = Environment.isExternalStorageEmulated();
System.out.println("Environment.isExternalStorageEmulated()=:" + isEmulated);
//设备的外存是否是可以拆卸的,比如SD卡,是则返回true。(API Level 9)
boolean isRemovable = Environment.isExternalStorageRemovable();
System.out.println("Environment.isExternalStorageRemovable()=:" + isRemovable);
}
public static void getApplicationDirectories(Context context) {
//获取当前程序路径 应用在内存上的目录 :/data/data/com.mufeng.toolproject/files
String filesDir = context.getFilesDir().toString();
System.out.println("context.getFilesDir()=:" + filesDir);
//应用的在内存上的缓存目录 :/data/data/com.mufeng.toolproject/cache
String cacheDir = context.getCacheDir().toString();
System.out.println("context.getCacheDir()=:" + cacheDir);
//应用在外部存储上的目录 :/storage/emulated/0/Android/data/com.mufeng.toolproject/files/Movies
String externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES).toString();
System.out.println("context.getExternalFilesDir()=:" + externalFilesDir);
//应用的在外部存储上的缓存目录 :/storage/emulated/0/Android/data/com.mufeng.toolproject/cache
String externalCacheDir = context.getExternalCacheDir().toString();
System.out.println("context.getExternalCacheDir()=:" + externalCacheDir);
//获取该程序的安装包路径 :/data/app/com.mufeng.toolproject-3.apk
String packageResourcePath = context.getPackageResourcePath();
System.out.println("context.getPackageResourcePath()=:" + packageResourcePath);
//获取程序默认数据库路径 :/data/data/com.mufeng.toolproject/databases/mufeng
String databasePat = context.getDatabasePath("mufeng").toString();
System.out.println("context.getDatabasePath(\"mufeng\")=:" + databasePat);
}
}