How to check availability of space on external storage?

后端 未结 6 1734
无人及你
无人及你 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:58

    /**
     * @return Number of bytes available on external storage
     */
    public static long getExternalStorageAvailableSpace() {
        long availableSpace = -1L;
        try {
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                    .getPath());
            stat.restat(Environment.getExternalStorageDirectory().getPath());
            availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return availableSpace;
    }
    

提交回复
热议问题