How to save image to album of cellphone in maui ?

Fei Xu 490 Reputation points
2023-06-29T14:57:27.5933333+00:00

Take picture or select and DownSize image then save it to FileSystem.CacheDirectory

now how can i copy it to album of cellphone on ios and android ?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,371 Reputation points Microsoft Vendor
    2023-06-30T05:21:14.5366667+00:00

    Hello,

    In MAUI, you can follow the steps below to save photo files to an album.

    For iOS:

    Step 1. Add the following permisson into plist.

    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>Please allow access to save photo in your photo library</string>
    

    Step 2. Create SavePictureService.cs in Platforms/iOS folder, and add the following code:

    public static class SavePictureService
    {
        public static void SavePicture(byte[] arr)
        {
            var imageData = NSData.FromArray(arr);
            var image = UIImage.LoadFromData(imageData);
    
            image.SaveToPhotosAlbum((img, error) =>
            {
            });
        }
    }
    

    For Android:

    Step 1. Add the following code into MainActivity.cs:

    public static MainActivity Instance { get; private set; } 
    
    // Then, make the assignment in the OnCreate method.
    Instance = this; 
    

    Step 2. Create SavePictureService.cs in Platforms/Android folder, and add the following code:

    public static class SavePictureService
    {
        public static bool SavePicture(byte[] arr, string imageName)
        {
            var contentValues = new ContentValues();
            contentValues.Put(MediaStore.IMediaColumns.DisplayName, imageName);
            contentValues.Put(MediaStore.Files.IFileColumns.MimeType, "image/png");
            contentValues.Put(MediaStore.IMediaColumns.RelativePath, "Pictures/relativePath");
            try
            {
                var uri = MainActivity.Instance.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, contentValues);
                var output = MainActivity.Instance.ContentResolver.OpenOutputStream(uri);
                output.Write(arr, 0, arr.Length);
                output.Flush();
                output.Close();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.ToString());
                return false;
            }
            contentValues.Put(MediaStore.IMediaColumns.IsPending, 1);
            return true;
        }
    }
    

    For MAUI, you could invoke those services in TakePhoto method to save photo into album.

    using Stream sourceStream = await photo.OpenReadAsync();
    using FileStream localFileStream = File.OpenWrite(localFilePath);
    // convert stream to byte[]
    MemoryStream ms = new MemoryStream();
    sourceStream.CopyTo(ms);
    var b = ms.ToArray();
    await sourceStream.CopyToAsync(localFileStream);
    
                    // save photo into album.
    #if ANDROID
                    MauiApp22.Platforms.Android.SavePictureService.SavePicture(b, photo.FileName);
    #elif IOS
                    MauiApp22.Platforms.iOS.SavePictureService.SavePicture(b);
    #endif
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Kevin Loggenberg 0 Reputation points
    2024-01-06T10:38:07.8533333+00:00

    Thank you for the above, this is the correct answer. I am a bit later to the party, but I though it would be worth mentioning. iOS is very fussy and the only way to satisfy what I can only assume is another security feature was to invoke the save function on the main thread.

    public static void SavePicture(byte[] arr)
    {
        var imageData = NSData.FromArray(arr);
        var image = UIImage.LoadFromData(imageData);
    
        MainThread.BeginInvokeOnMainThread(() =>
        {
            image.SaveToPhotosAlbum((img, error) =>
            {
            });
        });
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.