Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
@toms972
Copy link
Author

toms972 commented Jul 16, 2013

Loading

@dadja
Copy link

dadja commented Jan 23, 2015

+1

Loading

@ThatOtherVRGuy
Copy link

ThatOtherVRGuy commented Dec 19, 2016

Being a newb to Android-native with Unity, how is this used? Do we create DiskUtils.java in Plugins? Do we compile it to something?Are there dependencies?

Loading

@TTAliA
Copy link

TTAliA commented Mar 23, 2017

It should really return size in bytes not megabytes...

Loading

@jcowles
Copy link

jcowles commented Jun 15, 2017

In Unity:
AndroidJavaObject statFs = new AndroidJavaObject( "android.os.StatFs", Application.persistentDataPath);
long freeBytes = statFs.Call("getAvailableBytes");

Android reference:
https://developer.android.com/reference/android/os/StatFs.html#getAvailableBytes()

Loading

@13Flo
Copy link

13Flo commented Jun 22, 2017

@jcowles Thanks for your help. Though, the exact solution is as follow:
long freeBytes = statFs.Call<long>("getAvailableBytes");

;)

Loading

@RaschidJFR
Copy link

RaschidJFR commented Oct 10, 2017

Hi. Also newbie. So before calling those code lines you just drop this file in the Plugins folder inside de project I guess... where exactly?

Loading

@Danm72
Copy link

Danm72 commented Apr 10, 2018

Here it is using the new 'long' non deprecated APIs

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 = (statFs.getBlockCountLong() * statFs.getBlockSizeLong()) / 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.getAvailableBlocksLong();
        long blockSize = statFs.getBlockSizeLong();
        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.getBlockCountLong() * statFs.getBlockSizeLong());
        long free  = (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment