gpt4 book ai didi

android - 使用 Xamarin Forms nuget Xam.Plugin.Media 用相机拍照

转载 作者:行者123 更新时间:2023-11-29 00:19:53 32 4
gpt4 key购买 nike

我刚刚使用了 NUGET 并安装了 Xam.Plugin.Media,以便我可以使用我的移动应用拍照。

我按照xamarin组件网站中的示例代码,在按钮的点击事件中写了如下代码:

private async void btnTake1_Clicked(object sender, EventArgs e)
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}

var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{

Directory = "Sample",
Name = "test.jpg"
});

if (file == null)
return;

await DisplayAlert("File Location", file.Path, "OK");

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

}

一切顺利,解决方案构建成功,我现在只运行 UWP。但是当我点击按钮时,它会在

中的某个位置中断
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

有人可以帮我解决这个问题吗?提前致谢。沙林·戈帕兰

最佳答案

根据您的评论,以下内容适用于 Xamarin.Forms 和 UWP

在您的表单项目中,无论您想在何处显示图库选择器(我们通过单击按钮来完成),您都可以使用如下所示的内容:

private async void ChooseExistingClicked(object sender, EventArgs e)
{
bool hasPermission = false;

try
{
await CrossMedia.Current.Initialize();
hasPermission = CrossMedia.Current.IsPickPhotoSupported;
}
catch (Exception genEx)
{
var Error = genEx;
}

if (!hasPermission)
{
await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}

var file = await CrossMedia.Current.PickPhotoAsync();

if (file == null)
return;
image = file;
imagePanel.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}

您还需要正确设置权限。因此,在您的 UWP 项目中,您将看到一个名为“Package.appxmanifext”的文件,双击该文件,然后选择“功能”并确保选中列表中的“图片库”。

就是这样,这应该是您需要做的全部。

编辑:按照下面的要求,您需要为 native 平台设置访问照片库的权限。

在您的 iOS 项目中,您需要打开“Info.plist”文件的代码,该文件将显示一个 XML 表。你需要添加:

<key>NSCameraUsageDescription</key>
<string></string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This is used to upload an image of concern along with your report</string>
<key>NSMicrophoneUsageDescription</key>
<string></string>

标签之间的任何位置。

对于您的 Android 项目,您需要右键单击该项目并选择“属性”,这将打开一个新窗口,您需要在左侧选择“Android Manifest”,然后确保“CAMERA”、“READ_EXTERNAL_STORAGE”和'WRITE_EXTERNAL_STORAGE'。我相信这些是画廊和相机访问的主要部分。

关于android - 使用 Xamarin Forms nuget Xam.Plugin.Media 用相机拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44390483/

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