Exemplo n.º 1
0
        protected StorageInfo GetStorageInformation(string path)
        {
            StorageInfo storageInfo = new StorageInfo();

            Android.OS.StatFs stat   = new Android.OS.StatFs(path); //"/storage/sdcard1"
            long totalSpaceBytes     = 0;
            long freeSpaceBytes      = 0;
            long availableSpaceBytes = 0;

            /*
             * We have to do the check for the Android version, because the OS calls being made have been deprecated for older versions.
             * The ‘old style’, pre Android level 18 didn’t use the Long suffixes, so if you try and call use those on
             * anything below Android 4.3, it’ll crash on you, telling you that that those methods are unavailable.
             * http://blog.wislon.io/posts/2014/09/28/xamarin-and-android-how-to-use-your-external-removable-sd-card/
             */
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr2)
            {
                long blockSize = stat.BlockSizeLong;
                totalSpaceBytes     = stat.BlockCountLong * stat.BlockSizeLong;
                availableSpaceBytes = stat.AvailableBlocksLong * stat.BlockSizeLong;
                freeSpaceBytes      = stat.FreeBlocksLong * stat.BlockSizeLong;
            }
            else
            {
                totalSpaceBytes     = (long)stat.BlockCount * (long)stat.BlockSize;
                availableSpaceBytes = (long)stat.AvailableBlocks * (long)stat.BlockSize;
                freeSpaceBytes      = (long)stat.FreeBlocks * (long)stat.BlockSize;
            }

            storageInfo.TotalSpace     = totalSpaceBytes;
            storageInfo.AvailableSpace = availableSpaceBytes;
            storageInfo.FreeSpace      = freeSpaceBytes;
            return(storageInfo);
        }
Exemplo n.º 2
0
        public float FreeMemory()
        {
            Android.OS.StatFs statFs = new Android.OS.StatFs(Android.OS.Environment.RootDirectory.AbsolutePath);
            var  storage             = GetStorageInformation(Android.OS.Environment.RootDirectory.AbsolutePath);
            long Free = (statFs.AvailableBlocksLong * statFs.BlockSizeLong);

            //Android.OS.Environment.DataDirectory.AbsolutePath
            //Android.OS.Environment.ExternalStorageDirectory.Path
            Android.OS.StatFs stat = new Android.OS.StatFs(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal));
            long blockSize         = stat.BlockSizeLong;
            long availableBlocks   = stat.AvailableBlocksLong;
            long Free2             = (stat.AvailableBlocksLong * stat.BlockSizeLong);

            return(Free2 / 1024F / 1024F);
        }