Dana
5 天以前 ab80fd1f4154dcf65f009f8cd63d2a1b2037fedc
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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("yyyy_MM_dd", 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-外部存储
     * @param cameraid 0-android系统相机 1-P1 2-P2
     * @return 文件路径字符串
     */
    public static String getVideoFilePath(Context context, int tfCardFlag,int cameraid) {
        File file = getVideoFile(context, tfCardFlag,cameraid);
        return file != null ? file.getAbsolutePath() : null;
    }
}