1 package com.aib.com; 2 3 import java.io.File; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.os.Environment; 7 import android.os.StatFs; 8 import android.text.format.Formatter; 9 import android.util.Log; 10 import android.view.Menu; 11 import android.widget.TextView; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 getSDcardMemory(); 20 } 21 22 public void getSDcardMemory() { 23 /** 获取存储卡路径 */ 24 File sdcardDir = Environment.getExternalStorageDirectory(); 25 /** 获取手机内存路径 */ 26 // File Dir = Environment.getDataDirectory(); 27 /** StatFs 看文件系统空间使用情况 */ 28 StatFs statFs = new StatFs(sdcardDir.getPath()); 29 /** Block 的 size */ 30 int blockSize = statFs.getBlockSize(); 31 /** 总 Block 数量 */ 32 int totalBlocks = statFs.getBlockCount(); 33 /** 可用的 Block 数量 */ 34 int availableBlocks = statFs.getAvailableBlocks(); 35 /** 36 * android.text.format.Formatter :Utility class to aid in formatting 37 * common values that are not covered by the standard 38 * java.util.Formatter. 39 */ 40 Log.i("aib", blockSize +"---"+totalBlocks+"----"+availableBlocks); 41 String totaoSize = Formatter.formatFileSize(MainActivity.this, 42 totalBlocks * blockSize); 43 String availSize = Formatter.formatFileSize(MainActivity.this, 44 availableBlocks * blockSize); 45 TextView tv = (TextView) findViewById(R.id.tv); 46 tv.setText(sdcardDir.getPath()+ " 总空间: " 47 + totaoSize +" 可用空间:"+ availSize); 48 } 49 50 @Override 51 public boolean onCreateOptionsMenu(Menu menu) { 52 // Inflate the menu; this adds items to the action bar if it is present. 53 getMenuInflater().inflate(R.menu.main, menu); 54 return true; 55 } 56 57 }