本文实例讲述了Android实现获取SD卡总容量,可用大小,机身内存总容量及可用大小的方法。分享给大家供大家参考,具体如下:
可能有的同学不知道系统已经提供了获取获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法,用系统源代码提供的方法可以很简单,方便的获取其大小。这里我把系统源代码中的一个方法贴出来,供大家参考一下:
图片:
图片上红色方框中的代码就是源代码中获取SD卡大小的程序方法。
在这里我整理了一下,我把我整理封装好的方法给大家贴出来:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/** * 获得SD卡总大小 * * @return */ private String getSDTotalSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return Formatter.formatFileSize(MainActivity. this , blockSize * totalBlocks); } /** * 获得sd卡剩余容量,即可用大小 * * @return */ private String getSDAvailableSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(MainActivity. this , blockSize * availableBlocks); } /** * 获得机身内存总大小 * * @return */ private String getRomTotalSize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return Formatter.formatFileSize(MainActivity. this , blockSize * totalBlocks); } /** * 获得机身可用内存 * * @return */ private String getRomAvailableSize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(MainActivity. this , blockSize * availableBlocks); } |
希望本文所述对大家Android程序设计有所帮助。