gpt4 book ai didi

android - 如何授予xamarin相机权限

转载 作者:行者123 更新时间:2023-11-29 23:40:50 24 4
gpt4 key购买 nike

我想将相机添加到我的 xamarin 表单应用程序中。我看了这个视频。 https://www.youtube.com/watch?v=DJYLrVNY2ak&t=645s

完成所有工作后,我收到权限错误。

错误说:

未处理的异常:Plugin.Media.Abstractions.MediaPermissionException:需要相机权限

这段代码在 MainPage.xaml.cs 上

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Plugin.Media.Abstractions;
using Plugin.Media;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;

namespace camera
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

private async void Button_Clicked(object sender, EventArgs e)
{


await CrossMedia.Current.Initialize();

if (!CrossMedia.Current.IsCameraAvailable
|| !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", "No Camera available", "Ok");
return;
}

var file = await CrossMedia.Current.TakePhotoAsync(

new StoreCameraMediaOptions
{

SaveToAlbum = true,

});

if (file == null)
return;

PathLabel.Text = file.AlbumPath;

MainImage.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;


});




}
}
}

这段代码在 MainPage.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"
xmlns:local="clr-namespace:camera"
x:Class="camera.MainPage">

<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />

<Button Text="Take a Photo"
Clicked="Button_Clicked">

</Button>

<Image x:Name="MainImage"></Image>
<Label x:Name="PathLabel"></Label>


</StackLayout>

</ContentPage>

模拟器可以正常打开。当我单击“拍照”按钮时。我收到权限错误。

这是错误图片。 Error image

这是文件的链接。您可以下载该文件并查看错误。我想让这个应用程序做的就是拍摄一些细节,拍照并保存照片。文件链接 https://contedia-my.sharepoint.com/personal/muhammad_ikram_contedia_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fmuhammad%5Fikram%5Fcontedia%5Fcom%2FDocuments%2FPhotos%20%282%29%2Ezip&parent=%2Fpersonal%2Fmuhammad%5Fikram%5Fcontedia%5Fcom%2FDocuments&slrid=6f5b859e-50d6-0000-25d8-0fa7f8d3f9ce

最佳答案

将此行添加到 Android 中的 MainActivity

CrossCurrentActivity.Current.Init(this, savedInstanceState);

将这些行添加到您的代码 AsseblyInfo

[程序集:UsesFeature("android.hardware.camera", Required = false)]
[程序集:UsesFeature("android.hardware.camera.autofocus", Required = false)]

关于android - 如何授予xamarin相机权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51877812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com