package com.safeluck.floatwindow.util;
|
|
import android.content.Context;
|
import android.os.Environment;
|
|
import timber.log.Timber;
|
|
import java.io.File;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.Locale;
|
|
/**
|
* 视频文件工具类
|
* 用于创建和管理视频文件路径
|
*/
|
public class VideoFileUtils {
|
private static final String TAG = "VideoFileUtils";
|
private static final String VIDEO_DIR_NAME = "AnYun_VIDEO";
|
|
/**
|
* 获取视频文件保存目录
|
* @param context 上下文
|
* @param tfCardFlag 0-内部存储,1-外部存储
|
* @return 视频目录路径
|
*/
|
public static File getVideoDirectory(Context context, int tfCardFlag) {
|
File baseDir;
|
|
if (tfCardFlag == 1) {
|
// 外部存储 - 使用 FileUtil.getStoragePath 获取TF卡路径
|
String storagePath = FileUtil.getStoragePath(context, true);
|
if (storagePath == null || storagePath.isEmpty()) {
|
Timber.e("Failed to get external storage path");
|
return null;
|
}
|
baseDir = new File(storagePath);
|
} else {
|
// 内部存储
|
baseDir = Environment.getExternalStorageDirectory();
|
}
|
|
// 创建 AnYun_VIDEO 目录
|
File videoDir = new File(baseDir, VIDEO_DIR_NAME);
|
if (!videoDir.exists()) {
|
if (!videoDir.mkdirs()) {
|
Timber.e("Failed to create video directory: %s", videoDir.getAbsolutePath());
|
return null;
|
}
|
}
|
|
// 创建年月日目录(例如:260126)
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd", Locale.getDefault());
|
String dateDirName = dateFormat.format(new Date());
|
File dateDir = new File(videoDir, dateDirName);
|
if (!dateDir.exists()) {
|
if (!dateDir.mkdirs()) {
|
Timber.e("Failed to create date directory: %s", dateDir.getAbsolutePath());
|
return null;
|
}
|
}
|
|
return dateDir;
|
}
|
|
/**
|
* 生成视频文件名(时分秒.mp4)
|
* @return 文件名,例如:143025.mp4
|
*/
|
public static String generateVideoFileName(int usbCamId) {
|
SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss", Locale.getDefault());
|
if (usbCamId==2){
|
return timeFormat.format(new Date()) + "_P2.mp4";
|
}else if (usbCamId==1){
|
return timeFormat.format(new Date()) + "_P1.mp4";
|
}else{
|
return timeFormat.format(new Date()) + ".mp4";
|
}
|
|
}
|
|
/**
|
* 获取完整的视频文件路径
|
* @param context 上下文
|
* @param tfCardFlag 0-内部存储,1-外部存储
|
* @return 完整的文件路径
|
*/
|
public static File getVideoFile(Context context, int tfCardFlag,int usbCameraId) {
|
File dateDir = getVideoDirectory(context, tfCardFlag);
|
if (dateDir == null) {
|
return null;
|
}
|
|
String fileName = generateVideoFileName(usbCameraId);
|
return new File(dateDir, fileName);
|
}
|
|
/**
|
* 获取视频文件路径字符串 只能获取到内部相机文件的路径
|
* @param context 上下文
|
* @param tfCardFlag 0-内部存储,1-外部存储
|
* @return 文件路径字符串
|
*/
|
public static String getVideoFilePath(Context context, int tfCardFlag) {
|
File file = getVideoFile(context, tfCardFlag,0);
|
return file != null ? file.getAbsolutePath() : null;
|
}
|
}
|