|
4 | 4 | import android.app.Activity;
|
5 | 5 | import android.app.Fragment;
|
6 | 6 | import android.app.FragmentTransaction;
|
| 7 | +import android.content.ContentResolver; |
7 | 8 | import android.content.ContentValues;
|
8 | 9 | import android.content.Context;
|
9 | 10 | import android.content.Intent;
|
|
13 | 14 | import android.media.AudioDeviceInfo;
|
14 | 15 | import android.media.projection.MediaProjection;
|
15 | 16 | import android.media.projection.MediaProjectionManager;
|
| 17 | +import android.net.Uri; |
16 | 18 | import android.os.Build;
|
17 | 19 | import android.os.Build.VERSION;
|
18 | 20 | import android.os.Build.VERSION_CODES;
|
19 | 21 | import android.os.Bundle;
|
20 | 22 | import android.os.Handler;
|
21 | 23 | import android.os.Looper;
|
| 24 | +import android.os.ParcelFileDescriptor; |
22 | 25 | import android.os.ResultReceiver;
|
23 | 26 | import android.provider.MediaStore;
|
24 | 27 | import android.util.Log;
|
|
72 | 75 | import org.webrtc.audio.JavaAudioDeviceModule;
|
73 | 76 |
|
74 | 77 | import java.io.File;
|
| 78 | +import java.io.FileInputStream; |
| 79 | +import java.io.FileOutputStream; |
| 80 | +import java.io.InputStream; |
75 | 81 | import java.util.ArrayList;
|
76 | 82 | import java.util.HashMap;
|
77 | 83 | import java.util.List;
|
@@ -1046,22 +1052,64 @@ void startRecordingToFile(
|
1046 | 1052 | mediaRecorders.append(id, mediaRecorder);
|
1047 | 1053 | }
|
1048 | 1054 |
|
1049 |
| - void stopRecording(Integer id) { |
1050 |
| - MediaRecorderImpl mediaRecorder = mediaRecorders.get(id); |
1051 |
| - if (mediaRecorder != null) { |
1052 |
| - mediaRecorder.stopRecording(); |
1053 |
| - mediaRecorders.remove(id); |
1054 |
| - File file = mediaRecorder.getRecordFile(); |
1055 |
| - if (file != null) { |
1056 |
| - ContentValues values = new ContentValues(3); |
1057 |
| - values.put(MediaStore.Video.Media.TITLE, file.getName()); |
1058 |
| - values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4"); |
1059 |
| - values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath()); |
1060 |
| - applicationContext |
1061 |
| - .getContentResolver() |
1062 |
| - .insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); |
| 1055 | + void stopRecording(Integer id, String albumName) { |
| 1056 | + try { |
| 1057 | + MediaRecorderImpl mediaRecorder = mediaRecorders.get(id); |
| 1058 | + if (mediaRecorder != null) { |
| 1059 | + mediaRecorder.stopRecording(); |
| 1060 | + mediaRecorders.remove(id); |
| 1061 | + File file = mediaRecorder.getRecordFile(); |
| 1062 | + Uri collection; |
| 1063 | + |
| 1064 | + if (file != null) { |
| 1065 | + ContentValues values = new ContentValues(); |
| 1066 | + values.put(MediaStore.Video.Media.TITLE, file.getName()); |
| 1067 | + values.put(MediaStore.Video.Media.DISPLAY_NAME, file.getName()); |
| 1068 | + values.put(MediaStore.Video.Media.ALBUM, albumName); |
| 1069 | + values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4"); |
| 1070 | + values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000); |
| 1071 | + values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis()); |
| 1072 | + |
| 1073 | + //Android version above 9 MediaStore uses RELATIVE_PATH |
| 1074 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 1075 | + values.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + albumName); |
| 1076 | + values.put(MediaStore.Video.Media.IS_PENDING, 1); |
| 1077 | + |
| 1078 | + collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); |
| 1079 | + } else { |
| 1080 | + //Android version 9 and below MediaStore uses DATA |
| 1081 | + values.put(MediaStore.Video.Media.DATA, "/storage/emulated/0/Movies/" + albumName + "/" + file.getName()); |
| 1082 | + |
| 1083 | + collection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 1084 | + } |
| 1085 | + |
| 1086 | + ContentResolver resolver = applicationContext.getContentResolver(); |
| 1087 | + Uri uriSavedMedia = resolver.insert(collection, values); |
| 1088 | + |
| 1089 | + assert uriSavedMedia != null; |
| 1090 | + ParcelFileDescriptor pfd = resolver.openFileDescriptor(uriSavedMedia, "w"); |
| 1091 | + assert pfd != null; |
| 1092 | + FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor()); |
| 1093 | + |
| 1094 | + InputStream in = new FileInputStream(file); |
| 1095 | + |
| 1096 | + byte[] buf = new byte[8192]; |
| 1097 | + int len; |
| 1098 | + |
| 1099 | + while ((len = in.read(buf)) > 0) { |
| 1100 | + out.write(buf, 0, len); |
| 1101 | + } |
| 1102 | + |
| 1103 | + out.close(); |
| 1104 | + in.close(); |
| 1105 | + pfd.close(); |
| 1106 | + values.clear(); |
| 1107 | + } |
1063 | 1108 | }
|
| 1109 | + } catch(Exception e){ |
| 1110 | + |
1064 | 1111 | }
|
| 1112 | + |
1065 | 1113 | }
|
1066 | 1114 |
|
1067 | 1115 |
|
|
0 commit comments