Hello @Grime ,
Welcome to our Microsoft Q&A platform!
I don't know how to then store it to the media gallery so I can later pick it to work on it.
Do you mean saving the newly captured photos? The selected photos have been stored in the device, you could already pick them now. For this function, try using MediaPlugin instead. It provides the SaveToAlbum option when taking the new photo.
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = true
});
You can also achieve the function on each native platform. And then call the function code in the shared project using DependencyService.
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
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.