diff --git a/samples/ScreenRecordingSample/MainPage.xaml b/samples/ScreenRecordingSample/MainPage.xaml index 663d8c3..7409164 100644 --- a/samples/ScreenRecordingSample/MainPage.xaml +++ b/samples/ScreenRecordingSample/MainPage.xaml @@ -21,7 +21,7 @@ - diff --git a/src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs b/src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs index 8de94b3..bb39031 100644 --- a/src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs +++ b/src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs @@ -28,6 +28,8 @@ public partial class ScreenRecordingImplementation : MediaProjection.Callback, I public bool IsSupported => ProjectionManager is not null; + bool IsSavingToGallery; + public ScreenRecordingImplementation() { ProjectionManager = (MediaProjectionManager?)Platform.AppContext.GetSystemService(Context.MediaProjectionService); @@ -61,7 +63,18 @@ public void StartRecording(ScreenRecordingOptions? options = null) $"screenrecording_{DateTime.Now:ddMMyyyy_HHmmss}.mp4"); } - filePath = savePath; + if (saveOptions.SaveToGallery) + { + IsSavingToGallery = saveOptions.SaveToGallery; + Java.IO.File picturesDirectory = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures); + string fileName = Path.GetFileName(savePath); + Java.IO.File destinationFile = new Java.IO.File(picturesDirectory, fileName); + filePath = destinationFile.AbsolutePath; + } + else + { + filePath = savePath; + } Setup(); } @@ -189,6 +202,17 @@ public void SetUpMediaRecorder(bool enableMicrophone) MediaRecorder.SetVideoFrameRate(30); MediaRecorder.SetOutputFile(filePath); + if (IsSavingToGallery) + { + //This provides a way for applications to pass a newly created or downloaded media file to the media scanner service. + //The media scanner service will read metadata from the file and add the file to the media content provider. + //Source:https://developer.android.com/reference/android/media/MediaScannerConnection + MediaScannerConnection.ScanFile(Application.Context, + new string[] { filePath }, + new string[] { "video/mp4" }, + null); + } + try { MediaRecorder.Prepare();