Androidプログラミングでsdcardカードの情報を得る方法

1340 ワード

本明細書の例は、Androidプログラミングによってsdcardカード情報を取得する方法を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。

public static SDCardInfo getSDCardInfo() {
  String sDcString = android.os.Environment.getExternalStorageState();
  if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
   File pathFile = android.os.Environment.getExternalStorageDirectory();
   try {
    android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
    //   SDCard BLOCK  
    long nTotalBlocks = statfs.getBlockCount();
    //   SDCard   block SIZE
    long nBlocSize = statfs.getBlockSize();
    //          Block   
    long nAvailaBlock = statfs.getAvailableBlocks();
    //        Block   (               )
    long nFreeBlock = statfs.getFreeBlocks();
    SDCardInfo info = new SDCardInfo();
    //   SDCard      MB
    info.total = nTotalBlocks * nBlocSize;
    //    SDCard     MB
    info.free = nAvailaBlock * nBlocSize;
    return info;
   } catch (IllegalArgumentException e) {
    Log.e(LOG_TAG, e.toString());
   }
  }
  return null;
}
SDCardInfo sdCardInfo = Util.getSDCardInfo();
// sd    
sdCardInfo.total
// sd     
sdCardInfo.free

ここで述べたように、皆さんのAndroidプログラムの設計に役に立ちます。