AndroidRuntime: java.lang.IllegalArgumentException: Invalid UUID string: A436-0E05

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();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学安卓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值