Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public static float getSDCardFreeSize() {
        try {
            String path = Environment.getExternalStorageDirectory().getPath();
            StatFs fs = new StatFs(path);
            return calculateSizeInMB(fs);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    private static float calculateSizeInMB(StatFs stat) {
        if (stat != null) {
            return stat.getAvailableBlocks() * (stat.getBlockSize() / (1024f * 1024f));
        }
        return 0;
    }
}