sdcard Available Capacity MB - Android Hardware

Android examples for Hardware:SD Card

Description

sdcard Available Capacity MB

Demo Code


//package com.java2s;

import java.io.File;

import java.math.BigDecimal;

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

public class Main {
    @SuppressWarnings("deprecation")
    public static final float sdcardAvailableCapacityMB() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatFileSize(blockSize * availableBlocks);
    }/*w ww  . j a  v a 2 s .c  om*/

    private static float formatFileSize(double result) {
        result = result / 1024;
        result = result / 1024;
        BigDecimal bBigDecimal = new BigDecimal(result);
        bBigDecimal = bBigDecimal.setScale(2, 4);
        return bBigDecimal.floatValue();
    }
}

Related Tutorials