How to save video to album of cellphone in maui ?

Fei Xu 490 Reputation points
2023-06-30T06:27:35.4466667+00:00

recorded some video like xx.mp4 in FileSystem.CacheDirectory

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

Thanks

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-07-03T07:35:02.99+00:00

    Hello,

    You could refer to the following code to implement this feature on Android and iOS:

    For Android

    public static class SaveVideoService
    {
        public static bool SaveVideo(byte[] arr, string imageName)
        {
            var contentValues = new ContentValues();
            contentValues.Put(MediaStore.IMediaColumns.DisplayName, imageName);
            contentValues.Put(MediaStore.Files.IFileColumns.MimeType, "video/mp4");
            contentValues.Put(MediaStore.IMediaColumns.RelativePath, "Pictures/relativePath");
            try
            {
                var uri = MainActivity.Instance.ContentResolver.Insert(MediaStore.Video.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 iOS:

    public static class SaveVideoService
    {
        public static void SavePicture(string path)
        {
            var videoCompatible = UIVideo.IsCompatibleWithSavedPhotosAlbum(path);
            //Check that the video can be saved to an album
            if (videoCompatible)
            {
                UIVideo.SaveToPhotosAlbum(path, (s, e) => {
    
                });
            }
            else
            {
                Console.WriteLine("The video cannot be saved to an album");
            }
        }
    }
    

    You could invoke those services on MAUI as the following code:

    #if ANDROID
                MauiApp22.Platforms.Android.SaveVideoService.SaveVideo(b, "test.mp4");
    #elif IOS
                SaveVideoService.SaveVideo(localFilePath);
    #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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.