Check how much usable space is available at a given path. - Android android.os

Android examples for android.os:StatFs

Description

Check how much usable space is available at a given path.

Demo Code


//package com.book2s;
import java.io.File;

import android.annotation.SuppressLint;

import android.os.StatFs;

public class Main {
    /**/*from  w ww.  j  a  va2s . co m*/
     * Check how much usable space is available at a given path.
     * 
     * @param path
     *            The path to check
     * @return The space available in bytes
     */
    @SuppressLint("NewApi")
    public static long getUsableSpace(File path) {
        // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        // return path.getUsableSpace();
        // }
        final StatFs stats = new StatFs(path.getPath());
        return (long) stats.getBlockSize()
                * (long) stats.getAvailableBlocks();
    }
}

Related Tutorials