Question

I'm writing an app that gets how much disk space is left on an Android device and I use the following code:

public int phone_storage_free()
{
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
    return Free;
}

It returns 35mb, which is a tiny amount, so I assume it's the amount of disk space free to the app itself. So then I wrote some code to download a file bigger than 35mb, I saved the file to path.getPath()+"/test.avi" and it succeeded. I ran the above code again and got the same value of 35mb.

So my question is, how can I get the amount of internal disk space on the Android device available to the app that I can download to and it be accurate and something I can use.

Thanks!

Était-ce utile?

La solution

I found the solution. Use this code:

public long phone_storage_free()
{        
    long val=0;
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    val=availableBlocks * blockSize;
    return val;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top