如何獲取SD卡的大小

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);
      
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章