andorid storagetotalandfree-创新互联
                                            1 在Setting应用中,storage 显示total and free code:

final File path = VolumeInfo.getPath(); final long freeBytes = path.getFreeSpace(); final long totalBytes = path.getTotalSpace(); final long usedBytes = totalBytes - freeBytes; final String used = Formatter.formatFileSize(context, usedBytes); final String total = Formatter.formatFileSize(context, totalBytes);
2 VolumeInfo.java 是hidden的,只能通过反射得到。代码如下:
try {
    StorageManager sManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    Class class_StorageManager = StorageManager.class;
    Method method_getVolumes = class_StorageManager.getMethod("getVolumes");
    List得到数据: 08-18 17:28:31.087 32148-32148/ I/cyy1: freeBytes=190 MB 08-18 17:28:31.088 32148-32148/ I/cyy1: usedBytes=22.68 GB 08-18 17:28:31.088 32148-32148/ I/cyy1: id=private 08-18 17:28:31.088 32148-32148/ I/cyy1: freeBytes=9.90 MB 08-18 17:28:31.088 32148-32148 I/cyy1: usedBytes=22.84 GB 08-18 17:28:31.088 32148-32148/ I/cyy1: id=emulated
其中emulated 是external stroage的数据。而private 是data的数据
3 可以通过以下code验证:
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSizeLong();
long blockCount = stat.getBlockCountLong();
long availableBlocks = stat.getAvailableBlocksLong();
long avaibByte =   stat.getAvailableBytes();
Log.i("cyy","avaibByte="+Formatter.formatFileSize(context,avaibByte));
long free= stat.getFreeBytes();
Log.i("cyy","free="+Formatter.formatFileSize(context,free));
String totalSize = Formatter.formatFileSize(context, blockCount*blockSize);
String availableSize = Formatter.formatFileSize(context, availableBlocks*blockSize);
Environment.getExternalStorageDirectory() 跟emulated 数据一致;Environment.getDataDirectory()       
和private数据一致。另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:andorid storagetotalandfree-创新互联
网站URL:http://www.cqwzjz.cn/article/dgghec.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 