Hello,
Welcome to our Microsoft Q&A platform!
Please try to use my code to make a test.
Here is layout AXML code
<StackLayout>
<Button Text="take" Clicked="Button_Clicked"></Button>
<Image x:Name="MyImage" ></Image>
</StackLayout>
Here is layout background code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace App48
{
public partial class MainPage : ContentPage
{
string PhotoPath = "";
public MainPage()
{
InitializeComponent();
}
async Task TakePhotoAsync()
{
try
{
var photo = await MediaPicker.CapturePhotoAsync();
await LoadPhotoAsync(photo);
MyImage.Source = ImageSource.FromFile(PhotoPath);
Console.WriteLine($"CapturePhotoAsync COMPLETED: {PhotoPath}");
}
catch (Exception ex)
{
Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}
}
async Task LoadPhotoAsync(FileResult photo)
{
// canceled
if (photo == null)
{
PhotoPath = null;
return;
}
// save the file into local storage
var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using (var stream = await photo.OpenReadAsync())
using (var newStream = File.OpenWrite(newFile))
await stream.CopyToAsync(newStream);
PhotoPath = newFile;
}
private async void Button_Clicked(object sender, EventArgs e)
{
await TakePhotoAsync();
}
}
}
I make a test with above code, it work normally.
====================
Update====================
If you want to make the image to save the public folder, you should create a dependence service to set the path for different platforms, for example, in android, if you want to store this image to the Download folder, you can create a dependence service in the PCL.
public interface IExternalStorage
{
string GetPath();
}
In th android, you can achieve this interface.
[assembly: Dependency(typeof(AndroidImplementation))]
namespace App48.Droid
{
public class AndroidImplementation : IExternalStorage
{
public string GetPath()
{
Context context = Android.App.Application.Context;
var filePath = context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures); // Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;
string download = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
return download;
}
}
}
Add android:requestLegacyExternalStorage="true"
in the <application>
tag of AndroidManifest.xml
.
Then change var newFile = Path.Combine(DependencyService.Get<IExternalStorage>().GetPath(), photo.FileName);
in the LoadPhotoAsync()
method.
Here is running screenshot.
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
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.