How to check availability of space on external storage?

后端 未结 6 1692
无人及你
无人及你 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条回答
  •  执念已碎
    2021-01-08 00:59

    Here is how you get internal storage sizes:

     StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
     long blockSize = statFs.getBlockSize();
     long totalSize = statFs.getBlockCount()*blockSize;
     long availableSize = statFs.getAvailableBlocks()*blockSize;
     long freeSize = statFs.getFreeBlocks()*blockSize;
    

    Here is how you get external storage sizes (SD card size):

     StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
     long blockSize = statFs.getBlockSize();
     long totalSize = statFs.getBlockCount()*blockSize;
     long availableSize = statFs.getAvailableBlocks()*blockSize;
     long freeSize = statFs.getFreeBlocks()*blockSize;
    

提交回复
热议问题