I have a .Net MAUI (.NET 8) application that takes photos and I would like to save it in the gallery even if it is in the camera album. I have the following code to save the photo but it's on android.
public static async Task SaveImage(Stream imageStream)
{
using var stream = imageStream;
using var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
stream.Position = 0;
memoryStream.Position = 0;
#if ANDROID
var context = Platform.CurrentActivity;
if (OperatingSystem.IsAndroidVersionAtLeast(29))
{
string albumPath = Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath, "GPV2");
if (!Directory.Exists(albumPath))
{
Directory.CreateDirectory(albumPath);
}
Android.Content.ContentResolver resolver = context.ContentResolver;
Android.Content.ContentValues contentValues = new();
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.DisplayName, "image.png");
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.MimeType, "image/png");
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.RelativePath, "DCIM/" + "image");
Android.Net.Uri imageUri = resolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, contentValues);
var os = resolver.OpenOutputStream(imageUri);
Android.Graphics.BitmapFactory.Options options = new();
options.InJustDecodeBounds = true;
var bitmap = Android.Graphics.BitmapFactory.DecodeStream(stream);
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, os);
os.Flush();
os.Close();
}
else
{
Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
string path = System.IO.Path.Combine(storagePath.ToString(), "image.png");
System.IO.File.WriteAllBytes(path, memoryStream.ToArray());
var mediaScanIntent = new Android.Content.Intent(Android.Content.Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
context.SendBroadcast(mediaScanIntent);
}
#endif
}
I'll leave it here if it helps.
I'm a beginner in making applications for Apple devices like iOS and I don't know if I can do what I want.
I have all the camera and media permissions for the iPhone.
They have sent me the following link: https://learn.microsoft.com/en-us/answers/questions/1321167/how-to-save-image-to-album-of-cellphone-in-maui (Shivbaba's son).
I have put the following code:
public static void SaveImageToPhotosAlbums(Stream imageStream)
{
// Convert Stream to byte[]
byte[] imageData;
using (MemoryStream ms = new MemoryStream())
{
imageStream.CopyTo(ms);
imageData = ms.ToArray();
}
// Convert byte[] to NSData
var nsData = NSData.FromArray(imageData);
// Load UIImage from NSData
var image = UIImage.LoadFromData(nsData);
// Save image to Photos Album
image.SaveToPhotosAlbum((img, error) =>
{
if (error != null)
{
// Handle error
Console.WriteLine($"Error saving image: {error.LocalizedDescription}");
}
else
{
Console.WriteLine("Image saved successfully.");
}
});
}
And with this usings:
using Foundation; using Photos; using UIKit;
And that's works for me.