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.os.Environment;
import android.os.StatFs;

public class Main {
    private static String APP_DIR = "baidu";
    public static final File EXTERNAL_STORAGE_DIRECTORY = Environment.getExternalStorageDirectory();
    public static final int SD_MIN_AVAILAALE_SIZE = 2;

    public static boolean checkSDHasSpace() {
        try {
            StatFs statfs = new StatFs(EXTERNAL_STORAGE_DIRECTORY.getPath());
            long availaBlock = statfs.getAvailableBlocks();
            long blocSize = statfs.getBlockSize();
            long sdFreeSize = availaBlock * blocSize / 1024 / 1024;

            return sdFreeSize > SD_MIN_AVAILAALE_SIZE;
        } catch (Exception ex) {
            return false;
        }
    }

    public static String getPath(String appath) {
        String path = null;
        if (appath != null) {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + appath + "/";
        } else {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/";
        }
        return path;
    }
}