Android 查看可用存储内存大小
2010-05-24 21:06:00 来源:WEB开发网45StatFs stat = new StatFs(path.getPath());
46long blockSize = stat.getBlockSize();
47long totalBlocks = stat.getBlockCount();
48return totalBlocks * blockSize;
49} else {
50return ERROR;
51}
52}
53
54static public String formatSize(long size) {
55String suffix = null;
56
57if (size >= 1024) {
58suffix = "KiB";
59size /= 1024;
60if (size >= 1024) {
61suffix = "MiB";
62size /= 1024;
63}
64}
65
66StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
67
68int commaOffset = resultBuffer.length() - 3;
69while (commaOffset > 0) {
70resultBuffer.insert(commaOffset, ',');
71commaOffset -= 3;
72}
73
74if (suffix != null)
75resultBuffer.append(suffix);
76return resultBuffer.toString();
77}
78}
来个更简单的
view sourceprint? 01private void update() {
02File path = Environment.getExternalStorageDirectory();
03StatFs stat = new StatFs(path.getPath());
04long blockSize = stat.getBlockSize();
05long totalBlocks = stat.getBlockCount();
06long availableBlocks = stat.getAvailableBlocks();
07mTotalSize.setText(formatSize(totalBlocks * blockSize));
08mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
09mAvailableSize.setText(formatSize(availableBlocks * blockSize));
10
11
12private String formatSize(long size) {
13return Formatter.formatFileSize(this, size);
14}
更多精彩
赞助商链接