get Storage Info as a string - Android android.os

Android examples for android.os:Disk

Description

get Storage Info as a string

Demo Code

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

public class Main {

  public static String getStorageInfo(Context context) {
    File root = Environment.getRootDirectory();
    StatFs sf = new StatFs(root.getPath());
    long blockSize = sf.getBlockSize();
    long blockCount = sf.getBlockCount();
    long availCount = sf.getAvailableBlocks();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      File sdcardDir = Environment.getExternalStorageDirectory();
      StatFs sf1 = new StatFs(sdcardDir.getPath());
      blockSize += sf1.getBlockSize();/*from   w  w w.j  a v a 2  s  .  c  o  m*/
      blockCount += sf1.getBlockCount();
      availCount += sf1.getAvailableBlocks();
    }
    return Formatter.formatFileSize(context, availCount * blockSize) + " / "
        + Formatter.formatFileSize(context, blockSize * blockCount);
  }

}

Related Tutorials