[英]Taking a photo with camera using Xamarin Forms nuget Xam.Plugin.Media
I just used NUGET and installed Xam.Plugin.Media so that I can take photos using my mobile app. 我只是使用NUGET并安装了Xam.Plugin.Media,以便可以使用移动应用程序拍照。
I wrote the following code in the click event of the button as per the sample code in the xamarin component website: 我按照xamarin组件网站中的示例代码在按钮的click事件中编写了以下代码:
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;
});
}
All went well and the Solution is building successfully, I am running only UWP now. 一切顺利,解决方案正在成功构建,我现在仅运行UWP。 But when I click the button it breaks at some location in 但是当我单击按钮时,它在
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
Can somebody help me with this? 有人可以帮我吗? Thanks in advance. 提前致谢。 Sarin Gopalan 萨林·戈帕兰(Sarin Gopalan)
As per your comment the following applies to Xamarin.Forms and UWP 根据您的评论,以下内容适用于Xamarin.Forms和UWP
In your forms project, wherever it is you want to dispaly the gallery picker (we are doing it from a button click) you would use something like the below: 在您的Forms项目中,无论您要在何处显示图库选择器(我们通过单击按钮进行操作),都可以使用类似以下的内容:
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;
});
}
You also need to set the permissions up correctly. 您还需要正确设置权限。 So in your UWP project, you will see a file called 'Package.appxmanifext' double click on this, and select 'Capabilities' and make sure that 'Picture Library' in the list is ticked. 因此,在您的UWP项目中,您将看到一个名为“ Package.appxmanifext”的文件,双击该文件,然后选择“功能”,并确保已选中列表中的“图片库”。
That's it, that should be all you need to do. 就是这样,这应该是您需要做的所有事情。
EDIT: As requested below are the places you would need to set permissions for the native platforms to access the photo gallery. 编辑:根据下面的请求,您将需要为本地平台设置访问照片库的权限。
In your iOS project you will need to open the code of the 'Info.plist' file which will show you an XML sheet. 在您的iOS项目中,您需要打开“ Info.plist”文件的代码,该文件将显示XML表格。 you need to add: 您需要添加:
<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>
anywhere in between the tags. 标签之间的任何位置。
For your Android project you need to right click the project and select 'properties' this will open a new window, and on the left you need to select 'Android Manifest' then ensure that 'CAMERA', 'READ_EXTERNAL_STORAGE', and 'WRITE_EXTERNAL_STORAGE'. 对于您的Android项目,您需要右键单击该项目并选择“属性”,这将打开一个新窗口,在左侧,您需要选择“ Android清单”,然后确保“ CAMERA”,“ READ_EXTERNAL_STORAGE”和“ WRITE_EXTERNAL_STORAGE” 。 I believe those are the main one's for both gallery and camera access. 我相信这些是画廊和摄影机访问的主要工具。
Have you tried to InitializeComponent like the documentation on github? 您是否尝试过像github上的文档一样初始化InitializeComponent?
await CrossMedia.Current.Initialize();
https://github.com/jamesmontemagno/MediaPlugin https://github.com/jamesmontemagno/MediaPlugin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.