Home > android java > How to get the disk free size?

How to get the disk free size?

August 25th, 2009 Leave a comment Go to comments

(Android phone – how-to/example)

This is a little different from Java, Android has a separate package that partly deals with this problem.
Use
android.os.Environment to get the sdcard (if needed).
android.os.StatFs – << Retrieve overall information about the space on a filesystem. This is a Wrapper for Unix statfs(). >>

The function is straight forward:

public static long GetFreeSpaceB()
{
   try
   {
     String storageDirectory = Environment.getExternalStorageDirectory().toString();
     StatFs stat = new StatFs(storageDirectory);
     return stat.getAvailableBlocks() * stat.getBlockSize();
   }
   catch (Exception ex)
   {
     return -1;
   }
}

Categories: android java Tags:
  1. No comments yet.
  1. No trackbacks yet.