1.初始代码
public void getPackageSize(Context context,String packageName) {
@SuppressLint("WrongConstant") final StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(
Context.STORAGE_STATS_SERVICE);
final StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
final UserHandle user = android.os.Process.myUserHandle();
for (StorageVolume storageVolume : storageVolumes) {
final String uuidStr = storageVolume.getUuid();
final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
try {
Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(context) + " : " + storageVolume.getState());
Log.d("AppLog", "getFreeBytes:" + Formatter
.formatShortFileSize(context, storageStatsManager.getFreeBytes(uuid)));
Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getTotalBytes(uuid)));
Log.d("AppLog", "storage stats for app of package name:" + PACKAGE_NAME + " : ");
final StorageStats
storageStats = storageStatsManager.queryStatsForPackage(uuid, PACKAGE_NAME, user);
Log.d("AppLog", "getAppBytes:" + Formatter.formatShortFileSize(context, storageStats.getAppBytes()) +
" getCacheBytes:" + Formatter.formatShortFileSize(context, storageStats.getCacheBytes()) +
" getDataBytes:" + Formatter.formatShortFileSize(context, storageStats.getDataBytes()));
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.异常情况
09-29 22:08:25.435 8070 8070 E AndroidRuntime: FATAL EXCEPTION: main
09-29 22:08:25.435 8070 8070 E AndroidRuntime: Process: com.xxx.xxxx, PID: 8070
09-29 22:08:25.435 8070 8070 E AndroidRuntime: java.lang.IllegalArgumentException: Invalid UUID string: A436-0E05
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at java.util.UUID.fromString(UUID.java:194)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at com.xxx.xxxx.MainActivity.getPackageSize(MainActivity.java:281)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at com.xxx.xxxx.AppDataAdapter.onBindViewHolder(AppDataAdapter.java:59)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at com.xxx.xxxx.AppDataAdapter.onBindViewHolder(AppDataAdapter.java:22)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6735)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6777)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5706)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5973)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5812)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5808)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3878)
09-29 22:08:25.435 8070 8070 E AndroidRuntime: at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3290)
4.问题分析
uuid可能会出现不正常的情况,会抛出异常,可以使用try cacth进行捕获
5.解决后的代码
public void getPackageSize(Context context,String packageName) {
@SuppressLint("WrongConstant") final StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(
Context.STORAGE_STATS_SERVICE);
final StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
final UserHandle user = android.os.Process.myUserHandle();
for (StorageVolume storageVolume : storageVolumes) {
String uuidStr = storageVolume.getUuid();
UUID uuid = null;
try {
if (TextUtils.isEmpty(uuidStr)){
uuid = StorageManager.UUID_DEFAULT;
}else {
uuid = UUID.fromString(uuidStr);
}
}catch (Exception e){
uuid = StorageManager.UUID_DEFAULT;
}
try {
Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(context) + " : " + storageVolume.getState());
Log.d("AppLog", "getFreeBytes:" + Formatter
.formatShortFileSize(context, storageStatsManager.getFreeBytes(uuid)));
Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getTotalBytes(uuid)));
Log.d("AppLog", "storage stats for app of package name:" + PACKAGE_NAME + " : ");
final StorageStats
storageStats = storageStatsManager.queryStatsForPackage(uuid, PACKAGE_NAME, user);
Log.d("AppLog", "getAppBytes:" + Formatter.formatShortFileSize(context, storageStats.getAppBytes()) +
" getCacheBytes:" + Formatter.formatShortFileSize(context, storageStats.getCacheBytes()) +
" getDataBytes:" + Formatter.formatShortFileSize(context, storageStats.getDataBytes()));
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}