Android SD卡状态和剩余空间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public void getSDCardStateAndAvailableSize() {
2 String state = Environment.getExternalStorageState();
3 Log.d("xzy", "sd state is : " + state);
4
5 if (Environment.MEDIA_CHECKING.equals(state)) {
6 Log.i("xzy", "preparing");
7 }
8 if (!Environment.MEDIA_MOUNTED.equals(state)) {
9 Log.i("xzy", "unavailable");
10 return;
11 }
12
13 String direction = Environment.getExternalStorageDirectory().toString();
14 Log.i("xzy", "direction is : " + direction);
15
16 try {
17 StatFs stat = new StatFs(direction);
18 Log.i("xzy",
19 "available size is: " + stat.getAvailableBlocks() * (long) stat.getBlockSize() / 1024f /1024f + "MB");
20 } catch (Exception e) {
21 Log.i("xzy", "get sdcard useful size error");
22 }
23 }
2 String state = Environment.getExternalStorageState();
3 Log.d("xzy", "sd state is : " + state);
4
5 if (Environment.MEDIA_CHECKING.equals(state)) {
6 Log.i("xzy", "preparing");
7 }
8 if (!Environment.MEDIA_MOUNTED.equals(state)) {
9 Log.i("xzy", "unavailable");
10 return;
11 }
12
13 String direction = Environment.getExternalStorageDirectory().toString();
14 Log.i("xzy", "direction is : " + direction);
15
16 try {
17 StatFs stat = new StatFs(direction);
18 Log.i("xzy",
19 "available size is: " + stat.getAvailableBlocks() * (long) stat.getBlockSize() / 1024f /1024f + "MB");
20 } catch (Exception e) {
21 Log.i("xzy", "get sdcard useful size error");
22 }
23 }
版权所有: 博客园.Answer 转载请注明出处。www.cnblogs.com/xzy2046/
版权所有: 博客园.Answer 转载请注明出处。www.cnblogs.com/xzy2046/