Retrieve the total size of a disk. - Android android.os

Android examples for android.os:Disk

Description

Retrieve the total size of a disk.

Demo Code

import android.os.StatFs;

public class Main {

  /**//from  w  ww. j  a  va2  s .c om
   * Retrieve the total size of a disk.
   * 
   * @param diskPath
   *          the path of the disk.
   * @return the size of the disk in bytes.
   */
  public static long getDiskTotalSize(final String diskPath) {
    try {
      final StatFs stat = new StatFs(diskPath);
      final long blockSize = stat.getBlockSize();
      final long totalBlocks = stat.getBlockCount();
      final long totalSize = blockSize * totalBlocks;
      return totalSize;
    } catch (final Throwable e) {
      return 0;
    }
  }

}

Related Tutorials