Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import android.annotation.SuppressLint;

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

public class Main {
    @SuppressLint("NewApi")
    public static long getFreeDiskSpace() {
        String status = Environment.getExternalStorageState();
        long freeSpace = 0;
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long availableBlock = stat.getAvailableBlocksLong();
            freeSpace = availableBlock * blockSize / 1024;
        } else {
            return -1;
        }
        return freeSpace;
    }
}