微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 ExternalStorageStats

如何解决如何使用 ExternalStorageStats

嗨,谁能告诉我如何使用 ExternalStorageStats 类。基本上我想通过使用这个类获取图像字节、音频字节、视频字节、应用程序字节来获得存储使用量。顺便说一句,我通过使用 StorageStatsManager 类成功获得了总存储空间、免费存储空间、已用存储空间,但在互联网上没有找到如何使用 ExternalStorageStats获取图像字节、音频字节、视频字节、应用字节。我什至没有从官方文档中找到任何有用的内容,下面给出了官方文档的链接

https://developer.android.com/reference/android/app/usage/ExternalStorageStats

使用StorageStatsManager获取总存储量、可用存储量、已用存储量的代码如下

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","getTotalBytes:" + Formatter.formatShortFileSize(context,storageStatsManager.getTotalBytes(uuid)));
                    Log.d("AppLog","getUsedBytes:" + Formatter.formatShortFileSize(context,(storageStatsManager.getTotalBytes(uuid) - storageStatsManager.getFreeBytes(uuid))));
                    Log.d("AppLog","getFreeBytes:" + Formatter.formatShortFileSize(context,storageStatsManager.getFreeBytes(uuid)));
                    Log.d("AppLog","storage stats for app of package name:" + getPackageName() + " : ");

                    final StorageStats storageStats = storageStatsManager.queryStatsForPackage(uuid,getPackageName(),user);

                    Log.d("AppLog","getAppBytes:" + Formatter.formatShortFileSize(context,storageStats.getAppBytes()) +
                                    " getCacheBytes:" + Formatter.formatShortFileSize(context,storageStats.getCacheBytes()) +
                                    " getDataBytes:" + Formatter.formatShortFileSize(context,storageStats.getDataBytes()));


                                    try {
                                        textView.setText("Storage : " + storageVolume.getDescription(context) + " : " + storageVolume.getState() +
                                                "\n\n" + "Total Bytes : " + Formatter.formatShortFileSize(context,storageStatsManager.getTotalBytes(uuid)) +
                                                "\n\n" + "Used Bytes : " + Formatter.formatShortFileSize(context,(storageStatsManager.getTotalBytes(uuid) - storageStatsManager.getFreeBytes(uuid))) +
                                                "\n\n" + "Free Bytes : " + Formatter.formatShortFileSize(context,storageStatsManager.getFreeBytes(uuid)) +
                                                "\n\n" + "getAppBytes:" + Formatter.formatShortFileSize(context,storageStats.getAppBytes()) +
                                                "\n\n" + " getCacheBytes:" + Formatter.formatShortFileSize(context,storageStats.getCacheBytes()) +
                                                "\n\n" + " getDataBytes:" + Formatter.formatShortFileSize(context,storageStats.getDataBytes()) +
                                                "\n\n" + "storage stats for app of package name : " + getPackageName());


                                    } catch (IOException e) {
                                        e.printstacktrace();
                                    }

                                }
                            });

                        } catch (Exception e) {
                            e.printstacktrace();
                        }
                    }

解决方法

您可以按照以下函数中的说明使用 ExternalStorageStats。通过使用此函数,您将通过获取图像字节、音频字节、视频字节、应用程序字节来获得存储使用量。这里的函数是,但请记住,它仅适用于 API 级别 26 及以上。

@RequiresApi(api = Build.VERSION_CODES.O)
private void getResults(){

    StorageStatsManager storageStatsManager = (StorageStatsManager)getSystemService(Context.STORAGE_STATS_SERVICE);
    ExternalStorageStats externalStorageStats = null;
    UserHandle user = android.os.Process.myUserHandle();
    try {
        externalStorageStats = storageStatsManager.queryExternalStatsForUser(StorageManager.UUID_DEFAULT,user);
    } catch (IOException e) {
        e.printStackTrace();
    }
    long totalBytes = externalStorageStats.getTotalBytes();
    long imageBytes = externalStorageStats.getImageBytes();
    long audioBytes = externalStorageStats.getAudioBytes();
    long videoBytes = externalStorageStats.getVideoBytes();
    long appBytes = externalStorageStats.getAppBytes();

    String string= ("Total Bytes : " + getSize(totalBytes) +
                    "\n" + "Image Bytes : " + getSize(imageBytes) +
                    "\n" + "Audio Bytes : " + getSize(audioBytes) +
                    "\n" + "Video Bytes : " + getSize(videoBytes) +
                    "\n" + "App Bytes : " + getSize(appBytes));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。