wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.lechange.demo.tools;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.FileUtils;
import android.provider.MediaStore;
import android.view.WindowManager;
 
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
 
public class MediaPlayHelper {
 
    private final static String ProjectName = "LechangeDemo";
 
    private static Context mContext;
 
    public static void initContext(Context context){
        mContext = context;
    }
 
    public enum DHFilesType {
        DHImage,
        DHVideo,
        DHImageCache
    }
 
    public static void setFullScreen(Activity activity) {
        activity.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
 
    public static void quitFullScreen(Activity activity) {
        final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activity.getWindow().setAttributes(attrs);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
 
 
    /**
     * 创建文件路径
     *
     * @param file
     * @param filePath
     * @return
     */
    public static boolean createFilePath(File file, String filePath) {
        LogUtil.debugLog("20210513","filePath::"+filePath);
        int index = filePath.indexOf("/");
        if (index == -1) {
            LogUtil.debugLog("20210513","filePath---------:"+file.getAbsolutePath()+"  filepath::"+filePath);
            LogUtil.debugLog("20210513","filePath---------::"+file.getPath()+" "+file.getName()+" "+file.getParent());
           // scanFile(file);
            return false;
        }
        if (index == 0) {
            filePath = filePath.substring(index + 1, filePath.length());
            index = filePath.indexOf("/");
        }
        String path = filePath.substring(0, index);
        File fPath;
        if (file == null) {
            fPath = new File(path);
        } else {
            fPath = new File(file.getPath() + "/" + path);
        }
        if (!fPath.exists()) {
            if (!fPath.mkdir()) // SD卡已满无法在下载文件
            {
                return false;
            }
        }
        if (index < (filePath.length() - 1)) {
            String exPath = filePath.substring(index + 1, filePath.length());
            createFilePath(fPath, exPath);
        }
        return true;
    }
 
    /**
     * 生成抓图路径或录像存放路径
     */
    public static String getCaptureAndVideoPath(DHFilesType type, String cameraName) {
        String path = null;
        String picType = null;
        java.util.Date now = new java.util.Date();
        SimpleDateFormat tf = new SimpleDateFormat("yyyyMMddHHmmss");
        String sdPath = null;
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
            sdPath= mContext.getExternalFilesDir("demo").getAbsolutePath();
        }else{
            sdPath = Environment.getExternalStorageDirectory().getPath();
        }
 
 
        if (type == DHFilesType.DHImage) {
            picType = "image";
            path = sdPath + "/" + ProjectName + "/" + tf.format(now) + "_" + picType + "_"
                    + cameraName + ".jpg";
        } else {
            picType = "video";
            path = sdPath + "/" + ProjectName + "/" + tf.format(now) + "_" + picType + "_"
                    + cameraName + ".mp4";
        }
        if (type == DHFilesType.DHImageCache) {
            picType = "imageCache";
            path = sdPath + "/" + ProjectName + "/" + picType + "/" + tf.format(now) + "_"
                    + cameraName + ".jpg";
        }
        createFilePath(null, path);
        return path;
    }
 
 
    public static void updatePhotoAlbum(String path) {
        if (path==null)return;
        String[] split = path.split("/");
        String fileName = split[split.length-1];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
            ContentResolver contentResolver = mContext.getContentResolver();
            Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
 
            if (uri == null) {
                return;
            }
            OutputStream out = null;
            FileInputStream fis= null;
            try {
                out = contentResolver.openOutputStream(uri);
                fis = new FileInputStream(path);
                FileUtils.copy(fis, out);
                fis.close();
                out.close();
 
                //导出相册成功后,删除App沙盒内的临时文件
                MediaPlayHelper.delete(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            MediaScannerConnection.scanFile(mContext.getApplicationContext(), new String[]{path}, new String[]{"image/jpeg"}, new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
 
                }
            });
        }
    }
 
    public static void updatePhotoVideo(String path) {
        if (path==null)return;
        String[] split = path.split("/");
        String fileName = split[split.length-1];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Video.VideoColumns.DISPLAY_NAME, fileName);
            values.put(MediaStore.Video.VideoColumns.MIME_TYPE, "video/3gp");
            values.put(MediaStore.Video.VideoColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
            ContentResolver contentResolver = mContext.getContentResolver();
            Uri uri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
 
            if (uri == null) {
                return;
            }
            OutputStream out = null;
            FileInputStream fis = null;
            try {
                out = contentResolver.openOutputStream(uri);
                fis = new FileInputStream(path);
                FileUtils.copy(fis, out);
                fis.close();
                out.close();
 
                //导出相册成功后,删除App沙盒内的临时文件
                MediaPlayHelper.delete(path);
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            MediaScannerConnection.scanFile(mContext.getApplicationContext(), new String[]{path}, new String[]{"video/3gp"}, new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
 
                }
            });
        }
    }
 
 
    public static String getMimeType(File file) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(file.getName());
        return type;
    }
 
 
    /**
     * 生成下载录像存放路径
     */
    public static String getDownloadVideoPath(int type, String recordID, long startTime) {
        String path = null;
        String sdPath = null;
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
            sdPath= mContext.getExternalFilesDir("demo").getAbsolutePath();
        }else{
            sdPath = Environment.getExternalStorageDirectory().getPath();
        }
 
        String picType = "download";
        picType += type == 0 ? "_cloud" : "_remote";
        java.util.Date now = new java.util.Date(startTime);
        SimpleDateFormat tf = new SimpleDateFormat("yyyyMMddHHmmss");
        path = sdPath + "/" + ProjectName + "/" + tf.format(now) + "_" + picType + "_"
                + recordID + ".mp4";
        createFilePath(null, path);
        return path;
    }
 
    /**
     * 删除下载录像存放的录像
     */
    public static void deleteDownloadVideo(String recordID, long startTime) {
        String path = null;
        String sdPath = Environment.getExternalStorageDirectory().getPath();
        String picType = "download";
        java.util.Date now = new java.util.Date(startTime);
        SimpleDateFormat tf = new SimpleDateFormat("yyyyMMddHHmmss");
        path = sdPath + "/" + ProjectName + "/" + tf.format(now) + "_" + picType + "_"
                + recordID + ".mp4";
        File soFile = new File(path);
        if (soFile.exists()) {
            soFile.delete();
        }
    }
 
    /**
     * 删除缓存文件
     */
    public static void delete(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }
 
    public static BitmapDrawable picDrawable(String path) {
        try {
            FileInputStream fis = new FileInputStream(path);
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
            BitmapDrawable bd = new BitmapDrawable(bitmap);
            return bd;
        } catch (Throwable e) {
            return null;
        }
    }
}