From 284f3dbd343b8f40e921b4b887384ebc14ba3dcb Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期二, 23 十二月 2025 13:57:39 +0800
Subject: [PATCH] 1.h264文件写入的时候检查tfcard剩余空间 ,并执行删除
---
app/src/main/java/com/anyun/h264/H264Encoder.java | 41 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/app/src/main/java/com/anyun/h264/H264Encoder.java b/app/src/main/java/com/anyun/h264/H264Encoder.java
index 59525d2..9be6413 100644
--- a/app/src/main/java/com/anyun/h264/H264Encoder.java
+++ b/app/src/main/java/com/anyun/h264/H264Encoder.java
@@ -1,11 +1,13 @@
package com.anyun.h264;
+import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import com.anyun.libusbcamera.UsbCamera;
import com.anyun.libusbcamera.WatermarkParam;
import com.anyun.h264.model.WatermarkInfo;
+import com.anyun.h264.util.FileUtil;
import java.io.File;
import java.io.FileOutputStream;
@@ -82,6 +84,10 @@
private int cameraId = 1; // 鎽勫儚澶碔D锛岄粯璁や负1锛堢涓�涓憚鍍忓ご锛�
private long currentFileStartTime = 0; // 褰撳墠鏂囦欢鐨勫紑濮嬫椂闂达紙姣锛�
private static final long FILE_DURATION_MS = 60 * 1000; // 鏂囦欢鏃堕暱锛�1鍒嗛挓锛堟绉掞級
+
+ // Context 鍜屾竻鐞嗛厤缃�
+ private Context context; // Context 瀵硅薄锛岀敤浜庢竻鐞� TF 鍗℃枃浠�
+ private long maxH264TotalSizeGB = 5; // 鏈�澶� H264 鏂囦欢鎬诲ぇ灏忥紙GB锛夛紝榛樿 5GB
// 缃戠粶浼犺緭鎺у埗
private boolean enableNetworkTransmission = true; // 鏄惁鍚敤TCP/UDP缃戠粶浼犺緭
@@ -155,6 +161,22 @@
*/
public void setCameraId(int cameraId) {
this.cameraId = cameraId;
+ }
+
+ /**
+ * 璁剧疆 Context锛堢敤浜庢竻鐞� TF 鍗℃枃浠讹級
+ * @param context Context 瀵硅薄
+ */
+ public void setContext(Context context) {
+ this.context = context;
+ }
+
+ /**
+ * 璁剧疆鏈�澶� H264 鏂囦欢鎬诲ぇ灏忥紙GB锛�
+ * @param maxTotalSizeGB 鏈�澶ф�诲ぇ灏忥紙GB锛夛紝榛樿 5GB
+ */
+ public void setMaxH264TotalSizeGB(long maxTotalSizeGB) {
+ this.maxH264TotalSizeGB = maxTotalSizeGB;
}
/**
@@ -432,6 +454,25 @@
fileOutputStream = null;
}
+ // 妫�鏌ュ苟娓呯悊 TF 鍗′笂鐨� h264 鏂囦欢锛堝鏋滈渶瑕侊級
+ if (context != null && outputFileDirectory != null && !outputFileDirectory.isEmpty()) {
+ try {
+ // 鍒ゆ柇褰撳墠鐩綍鏄惁鍦� TF 鍗$殑 h264 鐩綍涓�
+ // 鐩綍缁撴瀯锛�/sdcard/h264/yyyyMMdd/锛宧264 鏍圭洰褰曞簲璇ユ槸鐖剁洰褰曠殑鐖剁洰褰�
+ File currentDir = new File(outputFileDirectory);
+ File parentDir = currentDir.getParentFile();
+ if (parentDir != null && "h264".equals(parentDir.getName())) {
+ // 褰撳墠鐩綍鍦� h264 鐩綍涓嬶紝鑾峰彇 h264 鏍圭洰褰�
+ String h264RootDir = parentDir.getAbsolutePath();
+ Timber.d("Checking and cleaning up h264 files in: %s", h264RootDir);
+ FileUtil.cleanupH264Files(context, h264RootDir, maxH264TotalSizeGB, 1);
+ }
+ } catch (Exception e) {
+ Timber.e(e, "Error during h264 files cleanup check");
+ // 娓呯悊澶辫触涓嶅奖鍝嶆枃浠跺垱寤猴紝缁х画鎵ц
+ }
+ }
+
// 鐢熸垚鏂版枃浠跺悕
long timeFile = System.currentTimeMillis() / 1000 * 1000;
currentFileStartTime = timeFile;
--
Gitblit v1.8.0