Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Creative Commons License 

import android.annotation.SuppressLint;

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

public class Main {
    /**
     * Number of bytes in one MB = 2<sup>20</sup>
     */
    public final static long SIZE_MB = 1024L * 1024L;

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public static long getExternalStorageFreeMemory() {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        double sdAvailSize;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            sdAvailSize = (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
        } else {
            sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
        }

        return Math.round(sdAvailSize / SIZE_MB);
    }
}