概述
洒家认为 结论先行
StorageStatsManager storageStatsManager = (StorageStatsManager)context.getSystemService(STORAGE_STATS_SERVICE);
try {
long totalBytes = storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT);//总空间大小
long availBytes = storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT);//可用空间大小
long systemBytes = totalBytes - availBytes;//系统所占不可用空间大小
} catch (IOException e) {
e.printStackTrace();
}
//想看外部存储时,替换uuid即可
转变大小时,需用1000的进制转换
public static String bytes2kb(long bytes){
int GB = 1000 * 1000 * 1000;
int MB = 1000 * 1000;
int KB = 1000;
//格式化小数
DecimalFormat format = new DecimalFormat("###.0");
if (bytes / GB >= 1){
return format.format(bytes / GB) + "GB";
}
else if (bytes / MB >= 1){
return format.format(bytes / MB) + "MB";
}
else if (bytes / KB >= 1){
return format.format(bytes / KB) + "KB";
}else {
return bytes + "B";
}
}
实际上的内存大小计算
public static String bytes2kb(long bytes){
int GB = 1024 * 1024 * 1024;
int MB = 1024 * 1024;
int KB = 1024;
//格式化小数
DecimalFormat format = new DecimalFormat("###.0");
if (bytes / GB >= 1){
return format.format(bytes / GB) + "GB";
}
else if (bytes / MB >= 1){
return format.format(bytes / MB) + "MB";
}
else if (bytes / KB >= 1){
return format.format(bytes / KB) + "KB";
}else {
return bytes + "B";
}
}
以下是洒家个人的记录,适合像我这样的小白观看~
1 从设置界面看到系统总空间大小
2 Settings/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
–>VolumeSizeCallbacks(StorageManager sm,StorageManagerVolumeProvider smvp—>VolumeSizesLoader(sm,smvp,mVolume))
3 src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java
–>getVolumeSize(StorageVolumeProvider storageVolumeProvider, StorageStatsManager stats, VolumeInfo info)->storageVolumeProvider.getTotalBytes(stats, info);
4 frameworks/base/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageVolumeProvider.java
–>getFreeBytes(StorageStatsManager stats, VolumeInfo volume)<<<StorageStatsManager.java
5 frameworks/base/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageManagerVolumeProvider.java
–>public long getFreeBytes(StorageStatsManager stats, VolumeInfo volume)–>return stats.getFreeBytes(volume.getFsUuid());
StorageStatsManager 能直接使用,参数只有一个return stats.getFreeBytes(volume.getFsUuid());
6 要获取系统的空间大小,自然想打印内置的volume的fsUuid,但打印多处都为空,继续跟return stats.getFreeBytes(volume.getFsUuid());看看是不是还有猫腻
7 frameworks/base/core/java/android/app/usage/StorageStatsManager.java
public @BytesLong long getFreeBytes(@NonNull UUID storageUuid) throws IOException {
try {
return mService.getFreeBytes(convert(storageUuid), mContext.getOpPackageName());
} catch (ParcelableException e) {
e.maybeRethrow(IOException.class);
throw new RuntimeException(e);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
UUID进行转化,在android.os.storage.StorageManager.convert中
8 frameworks/base/core/java/android/os/storage/StorageManager.java
–>
public static final String UUID_PRIVATE_INTERNAL = null;
/** {@hide} */
public static UUID convert(String uuid) {
if (Objects.equals(uuid, UUID_PRIVATE_INTERNAL)) {
return UUID_DEFAULT;
} else if (Objects.equals(uuid, UUID_PRIMARY_PHYSICAL)) {
return UUID_PRIMARY_PHYSICAL_;
} else if (Objects.equals(uuid, UUID_SYSTEM)) {
return UUID_SYSTEM_;
} else {
return UUID.fromString(uuid);
}
}
为null时进行了转换,直接调用就可以了
最后
以上就是曾经墨镜为你收集整理的android9获取系统空间大小,剩余空间大小,总空间大小的全部内容,希望文章能够帮你解决android9获取系统空间大小,剩余空间大小,总空间大小所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复