Wednesday, July 24, 2013

Android Getting Device Size in MB Programatically

     This Post helps to get the device Internal storage space,Freespace and Used Space


 // Return size is in Megabytes
     public class DeviceMemory {

        public static long getInternalStorageSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsol             utePath());
            //StatFs statFs = new StatFs("/data");
            long total = ((long)statFs.getBlockCount() * (long)statFs.getBlock            Size()) / 1048576;
            return total;
        }

        public static long getInternalFreeSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsol            utePath());
            //StatFs statFs = new StatFs("/data");
            long free  = ((long)statFs.getAvailableBlocks() * (long)statFs.get            BlockSize()) / 1048576;
            return free;
        }

        public static long getInternalUsedSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsol            utePath());
            //StatFs statFs = new StatFs("/data");
            long total = ((long)statFs.getBlockCount() * (long)statFs.getBlock            Size()) / 1048576;
            long free  = ((long)statFs.getAvailableBlocks() * (long)statFs.get            BlockSize()) / 1048576;
            long busy  = total - free;
            return busy;
        }
     }

No comments:

Post a Comment