Android安卓中循环录像并检测内存卡容量

时间:2021-10-05 05:21:59
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
     * 循环录像,当内存卡容量少于300M时,自动删除视频列表里面的第一个文件
     */
    private void xunhuanluxiang() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File path = Environment.getExternalStorageDirectory();
            // 取得sdcard文件路径
            StatFs statfs = new StatFs(path.getPath());
            // 获取block的SIZE
            long blocSize = statfs.getBlockSize();
            // 获取BLOCK数量
            long totalBlocks = statfs.getBlockCount();
            // 己使用的Block的数量
            long availaBlock = statfs.getAvailableBlocks();
            // 获取当前可用内存容量,单位:MB
            long sd = availaBlock * blocSize / 1024 / 1024;
            if (sd < 300) {
                String filepath = (Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/video/");
                File file = new File(filepath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File[] files = file.listFiles();
                if (files.length > 0) {
                    String childFile[] = file.list();
                    String dele = (filepath + childFile[0]);
                    File file2 = new File(dele);
                    file2.delete();
                }
            }
        } else if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_REMOVED)) {
            Toast.makeText(this, "请插入内存卡", Toast.LENGTH_SHORT).show();
        }
    }