Skip to content

Instantly share code, notes, and snippets.

@fuhoi
Forked from toms972/DiskUtils.java
Created March 4, 2016 03:37
Show Gist options
  • Save fuhoi/a3e658151b080bdecee3 to your computer and use it in GitHub Desktop.
Save fuhoi/a3e658151b080bdecee3 to your computer and use it in GitHub Desktop.

Revisions

  1. @toms972 toms972 created this gist Jul 16, 2013.
    67 changes: 67 additions & 0 deletions DiskUtils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    package com.tom.utils;

    import android.os.Environment;
    import android.os.StatFs;

    /**
    * Created by Tom on 7/15/13.
    * Some helper methods for FS queries.
    */
    public class DiskUtils {
    private static final long MEGA_BYTE = 1048576;

    /**
    * Calculates total space on disk
    * @param external If true will query external disk, otherwise will query internal disk.
    * @return Number of mega bytes on disk.
    */
    public static int totalSpace(boolean external)
    {
    StatFs statFs = getStats(external);
    long total = (((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize())) / MEGA_BYTE;
    return (int) total;
    }

    /**
    * Calculates free space on disk
    * @param external If true will query external disk, otherwise will query internal disk.
    * @return Number of free mega bytes on disk.
    */
    public static int freeSpace(boolean external)
    {
    StatFs statFs = getStats(external);
    long availableBlocks = statFs.getAvailableBlocks();
    long blockSize = statFs.getBlockSize();
    long freeBytes = availableBlocks * blockSize;

    return (int) (freeBytes / MEGA_BYTE);
    }

    /**
    * Calculates occupied space on disk
    * @param external If true will query external disk, otherwise will query internal disk.
    * @return Number of occupied mega bytes on disk.
    */
    public static int busySpace(boolean external)
    {
    StatFs statFs = getStats(external);
    long total = (statFs.getBlockCount() * statFs.getBlockSize());
    long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());

    return (int) ((total - free) / MEGA_BYTE);
    }

    private static StatFs getStats(boolean external){
    String path;

    if (external){
    path = Environment.getExternalStorageDirectory().getAbsolutePath();
    }
    else{
    path = Environment.getRootDirectory().getAbsolutePath();
    }

    return new StatFs(path);
    }

    }