下面列出了android.os.Environment#getRootDirectory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
synchronized (mRootsLock) {
for (RootInfo root : mRoots.values()) {
final RowBuilder row = result.newRow();
row.add(Root.COLUMN_ROOT_ID, root.rootId);
row.add(Root.COLUMN_FLAGS, root.flags);
row.add(Root.COLUMN_TITLE, root.title);
row.add(Root.COLUMN_DOCUMENT_ID, root.docId);
row.add(Root.COLUMN_PATH, root.path);
if(ROOT_ID_PRIMARY_EMULATED.equals(root.rootId)
|| root.rootId.startsWith(ROOT_ID_SECONDARY)
|| root.rootId.startsWith(ROOT_ID_PHONE)) {
final File file = root.rootId.startsWith(ROOT_ID_PHONE)
? Environment.getRootDirectory() : root.path;
row.add(Root.COLUMN_AVAILABLE_BYTES, file.getFreeSpace());
row.add(Root.COLUMN_CAPACITY_BYTES, file.getTotalSpace());
}
}
}
return result;
}
@Override
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
synchronized (mRootsLock) {
for (RootInfo root : mRoots.values()) {
final RowBuilder row = result.newRow();
row.add(Root.COLUMN_ROOT_ID, root.rootId);
row.add(Root.COLUMN_FLAGS, root.flags);
row.add(Root.COLUMN_TITLE, root.title);
row.add(Root.COLUMN_DOCUMENT_ID, root.docId);
row.add(Root.COLUMN_PATH, root.path);
if(ROOT_ID_PRIMARY_EMULATED.equals(root.rootId)
|| root.rootId.startsWith(ROOT_ID_SECONDARY)
|| root.rootId.startsWith(ROOT_ID_PHONE)) {
final File file = root.rootId.startsWith(ROOT_ID_PHONE)
? Environment.getRootDirectory() : root.path;
row.add(Root.COLUMN_AVAILABLE_BYTES, file.getFreeSpace());
row.add(Root.COLUMN_CAPACITY_BYTES, file.getTotalSpace());
}
}
}
return result;
}
@Override
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
synchronized (mRootsLock) {
for (RootInfo root : mRoots.values()) {
final RowBuilder row = result.newRow();
row.add(Root.COLUMN_ROOT_ID, root.rootId);
row.add(Root.COLUMN_FLAGS, root.flags);
row.add(Root.COLUMN_TITLE, root.title);
row.add(Root.COLUMN_DOCUMENT_ID, root.docId);
row.add(Root.COLUMN_PATH, root.path);
if(ROOT_ID_PRIMARY_EMULATED.equals(root.rootId)
|| root.rootId.startsWith(ROOT_ID_SECONDARY)
|| root.rootId.startsWith(ROOT_ID_PHONE)) {
final File file = root.rootId.startsWith(ROOT_ID_PHONE)
? Environment.getRootDirectory() : root.path;
row.add(Root.COLUMN_AVAILABLE_BYTES, file.getFreeSpace());
row.add(Root.COLUMN_CAPACITY_BYTES, file.getTotalSpace());
}
}
}
return result;
}
public FileSelector(final Context context, final FileOperation operation,
final OnHandleFileListener onHandleFileListener, final String[] fileFilters) {
mContext = context;
mOnHandleFileListener = onHandleFileListener;
final File sdCard = Environment.getExternalStorageDirectory();
if (sdCard.canRead()) {
mCurrentLocation = sdCard;
} else {
mCurrentLocation = Environment.getRootDirectory();
}
mDialog = new Dialog(context);
mDialog.setContentView(R.layout.dialog);
mDialog.setTitle(mCurrentLocation.getAbsolutePath());
prepareFilterSpinner(fileFilters);
prepareFilesList();
setSaveLoadButton(operation);
setNewFolderButton(operation);
setCancelButton();
}
@Override public String getFreeDiskPercentage() {
File rootDirectory = Environment.getRootDirectory();
StatFs fileSystemData = new StatFs(rootDirectory.getPath());
long blockSize;
long totalSize;
long availableSize;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = fileSystemData.getBlockSizeLong();
totalSize = fileSystemData.getBlockCountLong() * blockSize;
availableSize = fileSystemData.getAvailableBlocksLong() * blockSize;
} else {
blockSize = fileSystemData.getBlockSize();
totalSize = fileSystemData.getBlockCount() * blockSize;
availableSize = fileSystemData.getAvailableBlocks() * blockSize;
}
long freeDiskPercentage = availableSize * 100 / totalSize;
return String.valueOf(freeDiskPercentage);
}
/**
* calculates the dafault-path value for 2go.zip
*/
public static String getDefaultZipDirPath(Context context) {
File rootDir = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// before api-14/android-4.4/KITKAT
// write support on sdcard, if mounted
Boolean isSDPresent = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
rootDir = ((isSDPresent)) ? Environment.getExternalStorageDirectory() : Environment.getRootDirectory();
} else if (Global.USE_DOCUMENT_PROVIDER && (zipDocDirUri != null)) {
// DocumentFile docDir = DocumentFile.fromTreeUri(context, Uri.parse(zipDocDirUri));
DocumentFile docDir = DocumentFile.fromFile(new File(zipDocDirUri));
if ((docDir != null) && docDir.canWrite()) {
return rootDir.getAbsolutePath();
}
}
if (rootDir == null) {
// since android 4.4 Environment.getDataDirectory() and .getDownloadCacheDirectory ()
// is protected by android-os :-(
rootDir = getRootDir44();
}
final String zipfile = rootDir.getAbsolutePath() + "/copy";
return zipfile;
}
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Exception ignore) { /**/ }
return "";
}
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Exception ignore) { /**/ }
return "";
}
/**
* 获取 system prop 文件指定属性信息
* @param key 属性 key
* @return system prop 文件指定属性信息
*/
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Throwable ignore) {
}
return "";
}
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Exception ignore) {/**/}
return "";
}
private static String getSystemPropertyByStream(final String key) {
try {
Properties prop = new Properties();
FileInputStream is = new FileInputStream(
new File(Environment.getRootDirectory(), "build.prop")
);
prop.load(is);
return prop.getProperty(key, "");
} catch (Exception ignore) {/**/}
return "";
}
private void calculateInternalSpace() {
File root = Environment.getRootDirectory();
StatFs sf = new StatFs(root.getPath());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mInternalBlockSize = sf.getBlockSizeLong();
mInternalBlockCount = sf.getBlockCountLong();
mInternalAvailableBlocks = sf.getAvailableBlocksLong();
} else {
mInternalBlockSize = sf.getBlockSize();
mInternalBlockCount = sf.getBlockCount();
mInternalAvailableBlocks = sf.getAvailableBlocks();
}
}
private void calculateInternalSpace() {
File root = Environment.getRootDirectory();
StatFs sf = new StatFs(root.getPath());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mInternalBlockSize = sf.getBlockSizeLong();
mInternalBlockCount = sf.getBlockCountLong();
mInternalAvailableBlocks = sf.getAvailableBlocksLong();
} else {
mInternalBlockSize = sf.getBlockSize();
mInternalBlockCount = sf.getBlockCount();
mInternalAvailableBlocks = sf.getAvailableBlocks();
}
}
/**
* Storage details for internal, external, secondary and system partitions.
* External and secondary storage details are not exactly reliable.
*
* @return Thrift-compatible StorageDetails object
*/
public static StorageDetails getStorageDetails() {
StorageDetails sd = new StorageDetails();
// Internal
File path = Environment.getDataDirectory();
long[] internal = getStorageDetailsForPath(path);
if (internal.length == 2) {
sd.free = (int) internal[0];
sd.total = (int) internal[1];
}
// External
long[] external = getExternalStorageDetails();
if (external.length == 2) {
sd.freeExternal = (int) external[0];
sd.totalExternal = (int) external[1];
}
// Secondary
long[] secondary = getSecondaryStorageDetails();
if (secondary.length == 2) {
sd.freeSecondary = (int) secondary[0];
sd.totalSecondary = (int) secondary[1];
}
// System
path = Environment.getRootDirectory();
long[] system = getStorageDetailsForPath(path);
if (system.length == 2) {
sd.freeSystem = (int) system[0];
sd.totalSystem = (int) system[1];
}
return sd;
}
private String findDownLoadDirectory(){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
JUtils.Log("找到SD卡");
return Environment.getExternalStorageDirectory() + "/" + "download/";
}else{
JUtils.Log("没有SD卡");
return Environment.getRootDirectory() + "/" + "download/";
}
}
/**
* Gets the root storage directory of this device.
* @return
*/
static File getRootStorageDirectory() {
File ext = Environment.getExternalStorageDirectory();
if(ext == null){
ext = Environment.getRootDirectory();
return ext;
}
File parent = ext.getParentFile();
if (parent != null) {
return parent;
}
return ext;
}
private static String getMiuiVerName() {
final Properties prop = new Properties();
final File file = new File(Environment.getRootDirectory(), "build.prop");
try {
prop.load(new FileInputStream(file));
} catch (final IOException e) {
try { // Try again once more
prop.load(new FileInputStream(file));
} catch (final IOException ex) {
return "";
}
}
final String ver_name = prop.getProperty("ro.miui.ui.version.name");
return ver_name == null ? "" : ver_name;
}
public FileHandle(){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
rootDir = Environment.getExternalStorageDirectory();
} else {
rootDir = Environment.getRootDirectory();
}
}
@Override
protected TabsAdapter createTabsAdapter(DataHolder dataHolder) {
ListType.Sort sortMethod = new ListType.Sort(ListType.Sort.SORT_METHOD_PATH, true, true);
Bundle dcimFileListArgs = new Bundle();
String dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
dcimFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, dcim);
dcimFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);
Bundle directoryMusicFileListArgs = new Bundle();
String directoryMusic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
directoryMusicFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, directoryMusic);
directoryMusicFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);
Bundle directoryMoviesFileListArgs = new Bundle();
String directoryMovies = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
directoryMoviesFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, directoryMovies);
directoryMoviesFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);
Bundle externalStorageFileListArgs = new Bundle();
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
externalStorageFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, externalStorage);
externalStorageFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);
TabsAdapter tabsAdapter = new TabsAdapter(getActivity(), getChildFragmentManager())
.addTab(LocalMediaFileListFragment.class, dcimFileListArgs, R.string.dcim, 1)
.addTab(LocalMediaFileListFragment.class, directoryMusicFileListArgs, R.string.music, 2)
.addTab(LocalMediaFileListFragment.class, directoryMoviesFileListArgs, R.string.movies, 3)
.addTab(LocalMediaFileListFragment.class, externalStorageFileListArgs, R.string.external_storage, 4);
Environment.getRootDirectory();
File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(getActivity(),null);
for (int i = 0; i < externalFilesDirs.length; i++) {
File file = externalFilesDirs[i].getParentFile().getParentFile().getParentFile().getParentFile();
if (file.getAbsolutePath().equals(externalStorage))
continue;
Bundle bundle = new Bundle();
bundle.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, file.getAbsolutePath());
bundle.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);
tabsAdapter.addTab(LocalMediaFileListFragment.class, bundle, file.getName(),i+2);
}
return tabsAdapter;
}
/**
* 获取 Android 系统根目录 - path /system
* @return /system
*/
public File getRootDirectory() {
return Environment.getRootDirectory();
}