下面列出了android.os.storage.StorageManager#UUID_DEFAULT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* API 26 android O
* 获取总共容量大小,包括系统大小
*/
@SuppressLint("NewApi")
public static long getTotalSize(Context context, String fsUuid) {
try {
UUID id;
if (fsUuid == null) {
id = StorageManager.UUID_DEFAULT;
} else {
id = UUID.fromString(fsUuid);
}
StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
return stats.getTotalBytes(id);
} catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
e.printStackTrace();
return -1;
}
}
/**
* API 26 android O
* 获取总共容量大小,包括系统大小
*/
@RequiresApi(api = 26)
public long getTotalSize(String fsUuid) {
try {
UUID id;
if (fsUuid == null) {
id = StorageManager.UUID_DEFAULT;
} else {
id = UUID.fromString(fsUuid);
}
StorageStatsManager stats = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
return stats.getTotalBytes(id);
} catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
e.printStackTrace();
ToastUtils.show(this, "获取存储大小失败");
return -1;
}
}
/**
* API 26 android O
* 获取总共容量大小,包括系统大小
*/
@RequiresApi(Build.VERSION_CODES.O)
public static long getTotalSize(Context context, String fsUuid) {
try {
UUID id;
if (fsUuid == null) {
id = StorageManager.UUID_DEFAULT;
} else {
id = UUID.fromString(fsUuid);
}
StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
return stats.getTotalBytes(id);
} catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
e.printStackTrace();
return -1;
}
}
/**
* 获取存储块id
*
* @param fsUuid
* @return
*/
private UUID getUuid(String fsUuid) {
UUID id;
if (fsUuid == null) {
try {
id = StorageManager.UUID_DEFAULT;
} catch (NoSuchFieldError e) {
id = UUID.fromString("41217664-9172-527a-b3d5-edabb50a7d69");
}
} else {
id = UUID.fromString(fsUuid);
}
return id;
}
/**
* 获取应用的大小
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private static void getAppSizeAboveO(Context context, @NonNull OnGetSizeListener listener) {
StorageStatsManager storageStatsManager = (StorageStatsManager) context
.getSystemService(Context.STORAGE_STATS_SERVICE);
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
// 获取所有应用的StorageVolume列表
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
for (StorageVolume item : storageVolumes) {
String uuidStr = item.getUuid();
UUID uuid;
if (uuidStr == null) {
uuid = StorageManager.UUID_DEFAULT;
} else {
uuid = UUID.fromString(uuidStr);
}
int uid = getUid(context, context.getPackageName());
// 通过包名获取uid
StorageStats storageStats;
try {
storageStats = storageStatsManager.queryStatsForUid(uuid, uid);
AppSizeInfo ctAppSizeInfo = new AppSizeInfo();
ctAppSizeInfo.cacheSize = storageStats.getCacheBytes();
ctAppSizeInfo.dataSize = storageStats.getDataBytes();
ctAppSizeInfo.codeSize = storageStats.getAppBytes();
listener.onGetSize(ctAppSizeInfo);
} catch (IOException e) {
listener.onError(e);
}
}
}