可能有的同学不知道系统已经提供了获取获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法,用系统源代码提供的方法可以很简单,方便的获取其大小。这里我把系统源代码中的一个方法贴出来,供大家参考一下:
图片上
图片上红色方框中的代码就是源代码中获取SD卡大小的程序方法。
在这里我整理了一下,我把我整理封装好的方法给大家贴出来:
/** * 获得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); }转载请说明出处。
来源:http://blog.csdn.net/loongggdroid/article/details/12304695