Share via

taking pctures

Eduardo Gomez Romero 1,375 Reputation points
2024-10-28T23:43:09.2833333+00:00

I have this method and works fine

    async Task TakePhoto() {

        var photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo != null) {

            // Getting the file result information
            using Stream sourceStream = await photo.OpenReadAsync();

            // Saving the file in your local storage
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        }
    }

but for windows, I want to create a new folder like this

Pictures-> name of my application

in android I cannot find my pcturehttps://reccloud.com/u/g5blydh

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

Answer accepted by question author

Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,166 Reputation points Microsoft External Staff
2024-10-29T02:34:59.34+00:00

Hello,

string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);

The API path provided by FileSystem is a private path for the program. Its advantage is that files can be saved without permission, but its disadvantage is that it is not easy for users to find them.

For your expected behavior, you can refer to the following code to save the file to Pictures/AppName folder on Windows and save the picture to the photo album in Android.

For Windows:

async Task TakePhoto()
        {
            var photo = await MediaPicker.Default.CapturePhotoAsync();
            if (photo != null)
            {
 
                // Getting the file result information
                using Stream sourceStream = await photo.OpenReadAsync();
#if WINDOWS
                string localDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), AppInfo.Name);
                Console.WriteLine(localDirectoryPath);
                if (!Directory.Exists(localDirectoryPath))
                {
                    Directory.CreateDirectory(localDirectoryPath);
                }
                string localFilePath = Path.Combine(localDirectoryPath, photo.FileName);
#elif ANDROID
 
#endif
 
            }
        }

For Andorid:

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;
    }
}

Step 3. Save photo into Android album.


#elif ANDROID
// convert stream to byte[]
MemoryStream ms = new MemoryStream();
sourceStream.CopyTo(ms);
var b = ms.ToArray();


// save photo into android album.
MauiApp22.Platforms.Android.SavePictureService.SavePicture(b, photo.FileName);
#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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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