如何获取SD卡的大小

2023-05-01,,

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

《如何获取SD卡的大小.doc》

下载本文的Word格式文档,以方便收藏与打印。