Check if external storage has Space For certain Size - Android android.os

Android examples for android.os:External Storage

Description

Check if external storage has Space For certain Size

Demo Code

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class Main {

  private static final String TAG = "AlbumUtils";

  public static boolean hasSpaceForSize(long size) {
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) {
      return false;
    }/*from w  ww  . j  a v  a2 s.  c  om*/

    String path = Environment.getExternalStorageDirectory().getPath();
    try {
      StatFs stat = new StatFs(path);
      return stat.getAvailableBlocks() * (long) stat.getBlockSize() > size;
    } catch (Exception e) {
      Log.i(TAG, "Fail to access external storage", e);
    }
    return false;
  }

}

Related Tutorials