偶然发现显示不正常
\frameworks\base\core\java\android\os\FileUtils.java
public static @NonNull File[] listFilesOrEmpty(@Nullable File dir, FilenameFilter filter) {
if (dir == null) return EMPTY;
final File[] res = dir.listFiles(filter);
if (res != null) {
return res;
} else {
return EMPTY;
}
}
public static @Nullable File newFileOrNull(@Nullable String path) {
return (path != null) ? new File(path) : null;
}
+ public static long roundStorageSize(long size) {
+ long val = 1;
+ long pow = 1;
+ while ((val * pow) < size) {
+ val <<= 1;
+ if (val > 512) {
+ val = 1;
+ pow *= 1024;
+ }
+ }
+ return val * pow;
+ }
}
\frameworks\base\core\java\android\os\storage\StorageManager.java
public @NonNull StorageVolume getPrimaryStorageVolume() {
return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0];
}
/** {@hide} */
+ public long getPrimaryStorageSize() {
+ return FileUtils.roundStorageSize(Environment.getDataDirectory()
+ .getTotalSpace()+Environment.getRootDirectory().getTotalSpace());
+ }
/*
- public long getPrimaryStorageSize() {
- if(alternative_path){
- for (String path : INTERNAL_STORAGE_SIZE_PATHS_ALTERNATIVE) {
- final long numberBlocks = readLong(path);
- if (numberBlocks > 0) {
- return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
- }
- }
- }
- else{
- for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
- final long numberBlocks = readLong(path);
- if (numberBlocks > 0) {
- return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
- }
- }
- }
- return 0;
- }
*/
private long readLong(String path) {
try (final FileInputStream fis = new FileInputStream(path);
final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) {
return Long.parseLong(reader.readLine());
} catch (Exception e) {
Slog.w(TAG, "Could not read " + path, e);
return 0;
}
}
参考博客:
https://blog.youkuaiyun.com/layuetian2011/article/details/110110348