The function of storing the photo captured by the camera is not working, it is not saving to the cache, what can I do?

Davi Moura 1 Reputation point
2022-06-01T17:38:37.78+00:00

xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Aluno_Digital.CameraPage">
    <ContentPage.Content>
        <StackLayout>
            <Frame BackgroundColor="#2196F3" Padding="0" CornerRadius="0">
                <Label Text="ESCOLHA UMA OPÇÃO"
                TextColor="White"
                FontSize="20"
                HorizontalOptions="Center"></Label>
            </Frame>

            <Button Text="Escolher imagem" Clicked="EscolherIMG"></Button>
            <Button Text="Tirar Foto" Clicked="TirarFT"></Button>
            <Image x:Name="resultIMG" ></Image>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Aluno_Digital
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CameraPage : ContentPage
    {
        public CameraPage()
        {
            InitializeComponent();
        }

        async void EscolherIMG(System.Object sender, System.EventArgs e)
        {
            var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Por favor uma imagem"
            });
            var stream = await result.OpenReadAsync();
            resultIMG.Source = ImageSource.FromStream(() => stream);
        }
        async void TirarFT(System.Object sender, System.EventArgs e)
        {
            var result = await MediaPicker.CapturePhotoAsync();
            var stream = await result.OpenReadAsync();
            resultIMG.Source = ImageSource.FromStream(() => stream);
            var newFile = Path.Combine(FileSystem.AppDataDirectory, result.FileName);// in CacheDirectory, you could try to save in other folders
            using (var newStream = File.OpenWrite(newFile))
                await stream.CopyToAsync(newStream);

        }
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,302 questions
{count} votes