get available space on mounted media - Android Hardware

Android examples for Hardware:SD Card

Description

get available space on mounted media

Demo Code


//package com.java2s;

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

public class Main {

    public static long getAvailableSpace() {
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            StatFs statFs = new StatFs(Environment
                    .getExternalStorageDirectory().getPath());
            long blocksize = statFs.getBlockSize();
            long availableblock = statFs.getAvailableBlocks() - 4;

            return (long) ((availableblock > 0 ? availableblock : 0) * 1.0
                    * blocksize * 1.0);/*from ww  w.ja  va 2s . c o m*/
        }
        return 0;
    }
}

Related Tutorials