get Available Space on Disk Store - Android android.os

Android examples for android.os:Disk

Description

get Available Space on Disk Store

Demo Code

import java.io.IOException;

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

public class Main {

  public static long getAvailableStore() {

    String filePath = "/data";

    try {/* ww w .j  a v  a2s .  c  o m*/
      filePath = getCurrentAvailableStorageDir();
    } catch (IOException e) {
      e.printStackTrace();
    }

    StatFs statFs = new StatFs(filePath);

    long blocSize = statFs.getBlockSize();

    long availaBlock = statFs.getAvailableBlocks();

    long availableSpare = availaBlock * blocSize;

    return availableSpare;
  }

  public static String getCurrentAvailableStorageDir() throws IOException {
    if (isExternalStorageWriteable()) {
      return Environment.getExternalStorageDirectory().getCanonicalPath();
    } else {
      return Environment.getDataDirectory().getCanonicalPath();
    }
  }

  private static Boolean isExternalStorageWriteable() {
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) && Environment.getExternalStorageDirectory().canWrite()) {// ????????sd????????????
      mExternalStorageWriteable = true;
    }

    return mExternalStorageWriteable;
  }
}

Related Tutorials