Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.TargetApi;

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import java.io.File;

public class Main {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static long getSDFreeSize() {
        File path = Environment.getExternalStorageDirectory();
        if (path != null && path.exists() && path.isDirectory()) {
            StatFs sf = new StatFs(path.getPath());
            long blockSize = sf.getBlockSizeLong();
            long freeBlocks = sf.getAvailableBlocksLong();
            return (freeBlocks * blockSize) / 1024 / 1024;
        }
        return -1;
    }
}