Android 如何使用ExternalStorageStats

Android 如何使用ExternalStorageStats,android,storage,android-external-storage,Android,Storage,Android External Storage,嗨,有人能告诉我如何使用ExternalStorageStats类吗。基本上,我想通过使用这个类获取图像字节、音频字节、视频字节、应用程序字节来获取存储使用率。顺便说一下,我使用StorageStatsManager类成功地获得了总存储、可用存储和已用存储,但在internet上找不到如何使用ExternalStorageStats类获取图像字节、音频字节、视频字节和应用程序字节。我甚至没有从官方文档中找到任何有用的内容,下面给出了官方文档的链接 下面给出了使用StorageStatsMana

嗨,有人能告诉我如何使用ExternalStorageStats类吗。基本上,我想通过使用这个类获取图像字节、音频字节、视频字节、应用程序字节来获取存储使用率。顺便说一下,我使用StorageStatsManager类成功地获得了总存储、可用存储和已用存储,但在internet上找不到如何使用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();
                        }
                    }
final-StorageStatsManager-StorageStatsManager=(StorageStatsManager)getSystemService(Context.STORAGE\u STATS\u SERVICE);
最终的StorageManager StorageManager=(StorageManager)getSystemService(Context.STORAGE\u服务);
最终列表storageVolumes=storageManager.getStorageVolumes();
final UserHandle user=android.os.Process.myUserHandle();
用于(存储卷存储卷:存储卷){
最后一个字符串uuidStr=storageVolume.getUuid();
最终UUID UUID=uuidStr==null?StorageManager.UUID_默认值:UUID.fromString(uuidStr);
试一试{
Log.d(“AppLog”,“storage:“+uuid+”:”+storageVolume.getDescription(context)+“:”+storageVolume.getState());
Log.d(“AppLog”,“getTotalBytes:”+Formatter.formatShortFileSize(上下文,storageStatsManager.getTotalBytes(uuid));
Log.d(“AppLog”、“getUsedBytes:”+Formatter.formatShortFileSize(上下文)(storageStatsManager.getTotalBytes(uuid)-storageStatsManager.getFreeBytes(uuid));
Log.d(“AppLog”,“getFreeBytes:”+Formatter.formatShortFileSize(上下文,storageStatsManager.getFreeBytes(uuid));
Log.d(“AppLog”,“包名为“+getPackageName()+”:”)的应用程序的存储统计信息”;
final-StorageStats-StorageStats=storageStatsManager.queryStatsForPackage(uuid,getPackageName(),user);
Log.d(“AppLog”,“getAppBytes:”+Formatter.formatShortFileSize(上下文,storageStats.getAppBytes())+
getCacheBytes:“+Formatter.formatShortFileSize(上下文,storageStats.getCacheBytes())+
getDataBytes:“+Formatter.formatShortFileSize(上下文,storageStats.getDataBytes());
试一试{
textView.setText(“存储:”+storageVolume.getDescription(上下文)+“:”+storageVolume.getState()+
“\n\n”+”总字节数:“+Formatter.formatShortFileSize(上下文,storageStatsManager.getTotalBytes(uuid))+
“\n\n”+”使用的字节数:“+Formatter.formatShortFileSize(上下文,(storageStatsManager.getTotalBytes(uuid)-storageStatsManager.getFreeBytes(uuid)))+
“\n\n”+”空闲字节:“+Formatter.formatShortFileSize(上下文,storageStatsManager.getFreeBytes(uuid))+
“\n\n”+”getAppBytes:“+Formatter.formatShortFileSize(上下文,storageStats.getAppBytes())+
“\n\n”+”getCacheBytes:“+Formatter.formatShortFileSize(上下文,storageStats.getCacheBytes())+
“\n\n”+”getDataBytes:“+Formatter.formatShortFileSize(上下文,storageStats.getDataBytes())+
“\n\n”+”包名为“+getPackageName()”的应用程序的存储统计信息;
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
}捕获(例外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));
}

您可以使用
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));
}