Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android下獲取手機和SD卡的總空間和可用空間

Android下獲取手機和SD卡的總空間和可用空間

編輯:關於Android編程

獲取SD卡的總空間和可用空間

File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();

long totalSize = totalBlocks * blockSize;
long availSize = availableBlocks * blockSize;

String totalStr = Formatter.formatFileSize(this, totalSize);
String availStr = Formatter.formatFileSize(this, availSize);



獲取手機的總空間和可用空間

File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path2.getPath());
long blockSize2 = stat2.getBlockSize();
long totalBlocks2 = stat2.getBlockCount();
long availableBlocks2 = stat2.getAvailableBlocks();

long totalSize2 = totalBlocks2 * blockSize2;
long availSize2 = availableBlocks2 * blockSize2;

String totalStr2 = Formatter.formatFileSize(this, totalSize2);
String availStr2 = Formatter.formatFileSize(this, availSize2);

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved