Hello @AG ,
Welcome to our Microsoft Q&A platform!
how to save a picture from camera to device gallery using Xamarin.Essentials 1.7 Media Picker
Xamarin.Essentials.MediaPicker doesn't provide the function to save the captured phto to the gallery.
For this function, please try to save the file on each platform. Then call the function code in the shared project using DependencyService. Here is sample code, you could refer to:
private async void Button_Clicked(object sender, EventArgs e)
{
var photo = await MediaPicker.CapturePhotoAsync();
var stream = await photo.OpenReadAsync();
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
var bytes = ms.ToArray();
DependencyService.Get<ITestInterface>().StorePhotoToGallery(bytes, "test.png");
}
//platform implementation
[assembly: Xamarin.Forms.Dependency(typeof(TestImplementation))]
namespace TestApplication_5.Droid
{
public class TestImplementation : ITestInterface
{
public async void storePhotoToGallery(byte[] bytes, string fileName)
{
Context context = MainActivity.Instance;
try
{
Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
string path = System.IO.Path.Combine(storagePath.ToString(), fileName);
System.IO.File.WriteAllBytes(path, bytes);
var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
context.SendBroadcast(mediaScanIntent);
}
catch (Exception ex)
{
}
}
}
}
For the iOS implementation, you could refer to: https://stackoverflow.com/a/60630182/11083277
You could also MediaPlugin instead. It provides the SaveToAlbum option when taking the new photo.
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = true
});
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.