-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add SaveToGallery option to MediaPickerOptions for capture operations #34641
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
base: net11.0
Are you sure you want to change the base?
Changes from all commits
34033fe
6fee633
9dea60b
5f4436b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ | |
| } | ||
| else | ||
| { | ||
| if (!pickExisting) | ||
| if (!pickExisting && options?.SaveToGallery == true) | ||
| { | ||
| await Permissions.EnsureGrantedAsync<Permissions.PhotosAddOnly>(); | ||
| } | ||
|
|
@@ -169,6 +169,12 @@ | |
| PickerRef?.Dispose(); | ||
| PickerRef = null; | ||
|
|
||
| // Save captured media to the photo gallery if requested | ||
| if (!pickExisting && result is not null && options?.SaveToGallery == true) | ||
| { | ||
| await SaveToPhotoLibraryAsync(result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
@@ -481,6 +487,44 @@ | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Saves the captured media file to the device's photo library using PHPhotoLibrary. | ||
| /// </summary> | ||
| static async Task SaveToPhotoLibraryAsync(FileResult fileResult) | ||
| { | ||
| try | ||
| { | ||
| using var stream = await fileResult.OpenReadAsync(); | ||
| var extension = System.IO.Path.GetExtension(fileResult.FileName); | ||
| var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"{Guid.NewGuid()}{extension}"); | ||
| using (var fileStream = File.Create(tempPath)) | ||
| { | ||
| stream.Position = 0; | ||
| await stream.CopyToAsync(fileStream); | ||
| } | ||
|
Comment on lines
+497
to
+504
|
||
|
|
||
| var url = NSUrl.FromFilename(tempPath); | ||
|
|
||
| await PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAsync(() => | ||
|
Check failure on line 508 in src/Essentials/src/MediaPicker/MediaPicker.ios.cs
|
||
| { | ||
| if (IsImageFile(fileResult.FileName)) | ||
| { | ||
| PHAssetChangeRequest.FromImage(url); | ||
| } | ||
| else | ||
| { | ||
| PHAssetChangeRequest.FromVideo(url); | ||
| } | ||
| }); | ||
|
|
||
| try { File.Delete(tempPath); } catch (IOException) { } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| System.Diagnostics.Debug.WriteLine($"Failed to save to photo library: {ex.Message}"); | ||
| } | ||
| } | ||
|
|
||
| class PhotoPickerDelegate : UIImagePickerControllerDelegate | ||
| { | ||
| public Action<NSDictionary> CompletedHandler { get; set; } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The XML summary says that on Android versions below API 29 this method uses a direct file copy, but the implementation still uses a MediaStore insert + OpenOutputStream for all API levels. Please either adjust the comment to match the actual approach, or implement the documented pre-29 behavior so the comment stays accurate (and make sure the pre-29 path results in the media being visible in the gallery).