循环录像,听起来是不是很高大上,其实原理就是检测内存卡容量,少于一定值就自动删除指定的文件夹,这个做监控或者行车记录仪会用得着。


/**
	 * 循环录像,当内存卡容量少于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();
		}
	}