0

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

4
  • Could you tell us which platform you get the error on, whether it's iOS, android or UWP... It's likely that you've simply missed out the permissions request on one of them. Commented Jun 6, 2017 at 12:52
  • @Digitalsa1nt Sorry I forgot to say that....I am getting this error on UWP. Commented Jun 6, 2017 at 12:54
  • I can see the code for permission in the following URL: components.xamarin.com/gettingstarted/MediaPlugin/true Put how do I enforce that? Where do I need to add this code snippet. Commented Jun 6, 2017 at 13:01
  • I've added an answer below, that should help you with your problem. Commented Jun 6, 2017 at 13:10

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Can you please guide me how to set permissions in case required for android and IOS as well?
@SarinGopalan I've amended my answer to include both iOS and Android permissions. If it qualifies as answering your question, please feel free to tick it as the answer. =)
0

Have you tried to InitializeComponent like the documentation on github?

await CrossMedia.Current.Initialize(); 

https://github.com/jamesmontemagno/MediaPlugin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.