Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Android devices to Save to Gallery #13

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/ScreenRecordingSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</HorizontalStackLayout>
<HorizontalStackLayout Spacing="5">
<Switch x:Name="saveToGallery" VerticalOptions="Center" />
<Label Text="Save to gallery (iOS/macOS only)" VerticalOptions="Center" />
<Label Text="Save to gallery" VerticalOptions="Center" />
</HorizontalStackLayout>
<HorizontalStackLayout Spacing="5">
<Switch x:Name="setCustomNotification" VerticalOptions="Center" Toggled="OnToggled"/>
Expand Down
26 changes: 25 additions & 1 deletion src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

public bool IsSupported => ProjectionManager is not null;

public bool IsSavingToGallery { get; private set; }
jfversluis marked this conversation as resolved.
Show resolved Hide resolved

public ScreenRecordingImplementation()
{
ProjectionManager = (MediaProjectionManager?)Platform.AppContext.GetSystemService(Context.MediaProjectionService);
Expand Down Expand Up @@ -61,7 +63,18 @@
$"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);

Check warning on line 69 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-sample-ci

Converting null literal or possible null value to non-nullable type.

Check warning on line 69 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-sample-ci

Converting null literal or possible null value to non-nullable type.

Check warning on line 69 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-plugin-ci

Converting null literal or possible null value to non-nullable type.

Check warning on line 69 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-plugin-ci

Converting null literal or possible null value to non-nullable type.
string fileName = Path.GetFileName(savePath);
Java.IO.File destinationFile = new Java.IO.File(picturesDirectory, fileName);
filePath = destinationFile.AbsolutePath;
}
else
{
filePath = savePath;
}

Setup();
}
Expand Down Expand Up @@ -189,6 +202,17 @@
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 },

Check warning on line 211 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-sample-ci

Possible null reference assignment.

Check warning on line 211 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-sample-ci

Possible null reference assignment.

Check warning on line 211 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-plugin-ci

Possible null reference assignment.

Check warning on line 211 in src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs

View workflow job for this annotation

GitHub Actions / build-plugin-ci

Possible null reference assignment.
new string[] { "video/mp4" },
null);
}

try
{
MediaRecorder.Prepare();
Expand Down
Loading