本文主要是介绍Android Environment 获取的路径问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 以获取 /System 路径为例
/*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDROID_ROOT;}// ENV_ANDROID_ROOT 的值为 "ANDROID_ROOT"// private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");static File getDirectory(String variableName, String defaultPath) {// 获取环境变量 ANDROID_ROOT 对应的值String path = System.getenv(variableName);return path == null ? new File(defaultPath) : new File(path);}
1.1 获取环境变量 ANDROID_ROOT 对应的值
环境变量可以简单理解为可以在进程中获取的键值对。
2. 其他路径也是类似
private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");private static final String DIR_ANDROID_DATA_PATH = getDirectoryPath(ENV_ANDROID_DATA, "/data");private static final File DIR_ANDROID_DATA = new File(DIR_ANDROID_DATA_PATH);private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");private static final File DIR_METADATA = new File("/metadata");private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,"/system_ext");private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT,"/apex");
这篇关于Android Environment 获取的路径问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!