android系统容量信息用Environment和StaFs两个类来获取,具体方法如下:
1.Environment
File data = Environment.getDataDirectory();//获取手机存储文件对象
File data = Environment.getExternalStorageDirectory();//获取sdcard文件对象
File data = Environment.getExternalStorageDirectory();//获取系统root文件对象
2.StatFs:
StatFs sdStatFs = new StatFs(data.getPath());
int availableBlocks = statFs.getAvailableBlocks();// 可用存储块的数量
int blockCount = statFs.getBlockCount();// 总存储块的数量
int size = statFs.getBlockSize();// 每块存储块的大小
int totalSize = blockCount * size;// 总存储量
int availableSize = availableBlocks * size;// 可用容量
- public String phoneCapacity() {
- File data = Environment.getDataDirectory();
- StatFs statFs = new StatFs(data.getPath());
- int availableBlocks = statFs.getAvailableBlocks();
- int blockCount = statFs.getBlockCount();
- int size = statFs.getBlockSize();
- int totalSize = blockCount * size;
- int availableSize = availableBlocks * size;
- String phoneCapacity = Integer.toString(availableSize / 1024 / 1024)
- + "MB/" + Integer.toString(totalSize / 1024 / 1024) + "MB";
- return phoneCapacity;
- }
- public String sdcardCapacity() {
- File sdData = Environment.getExternalStorageDirectory();
- StatFs sdStatFs = new StatFs(sdData.getPath());
- int sdAvailableBlocks = sdStatFs.getAvailableBlocks();
- int sdBlockcount = sdStatFs.getBlockCount();
- int sdSize = sdStatFs.getBlockSize();
- int sdTotalSize = sdBlockcount * sdSize;
- int sdAvailableSize = sdAvailableBlocks * sdSize;
- String sdcardCapacity = Integer.toString(sdAvailableSize / 1024 / 1024)
- + "MB/" + Integer.toString(sdTotalSize / 1024 / 1024) + "MB";
- return sdcardCapacity;
- }
阅读(1848) | 评论(0) | 转发(0) |