Xamarin.Forms拍照保存到本地Camera相册

仅限Android

参照官方文档:https://docs.microsoft.com/zh-cn/xamarin/essentials/media-picker?context=xamarin%2Fxamarin-forms&tabs=android

1、修改权限:

 打开AndroidManifest.xml 文件,manifest”节点内添加以下代码 :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

2、样例代码:

async Task TakePhotoAsync()
{
    try
    {
        var photo = await MediaPicker.CapturePhotoAsync();
        await LoadPhotoAsync(photo);
        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;
}

3、修改路径

var newFile = Path.Combine("/storage/emulated/0/DCIM/Camera/", photo.FileName);
原文地址:https://www.cnblogs.com/kwoon/p/14330472.html