How to check availability of space on external storage?

后端 未结 6 1730
无人及你
无人及你 2021-01-08 00:24

How do you check if the SD card is full or not so that your application can decide if it can continue to do its job i.e. write to external storage or notify the user that st

6条回答
  •  旧时难觅i
    2021-01-08 00:54

    /******************************************************************************************
    Returns size in MegaBytes.
    
    If you need calculate external memory, change this: 
       StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    to this: 
       StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
    
    ******************************************************************************************/
        public long totalMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
            long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
            return total;
        }
    
        public long freeMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
            long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
            return free;
        }
    
        public long busyMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
            long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
            long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
            long   busy   = total - free;
            return busy;
        }
    

提交回复
热议问题