简体   繁体   中英

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.

I wrote the following code in the click event of the button as per the sample code in the xamarin component website:

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. 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

As per your comment the following applies to Xamarin.Forms and 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:

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.

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. 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'. I believe those are the main one's for both gallery and camera access.

Have you tried to InitializeComponent like the documentation on github?

await CrossMedia.Current.Initialize();

https://github.com/jamesmontemagno/MediaPlugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM