public class DemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize(); //每一个区块的大小
long totalBlocks = stat.getBlockCount(); //所有区块
long availableBlocks = stat.getAvailableBlocks(); // 所有可用区块
String totalsize = Formatter.formatFileSize(this, totalBlocks * blockSize);
String availablesize = Formatter.formatFileSize(this, availableBlocks * blockSize);
TextView tv = new TextView(this);
tv.setText("sd卡大小"+totalsize+"\n"+"可用空间"+availablesize);
setContentView(tv);
}
}
转载于:https://blog.51cto.com/uxlwsfmf/1212345