Chin
Chin

Reputation: 20675

UnauthorizedAccessException in Webcam app

I'm trying to make a simple webcam app:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.MediaProperties;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace App1
{
public sealed partial class MainPage : Page
{
    private Windows.Media.Capture.MediaCapture m_mediaCaptureMgr;
    private Windows.Storage.StorageFile m_photoStorageFile;
    private readonly String PHOTO_FILE_NAME = "photo.jpg";

    public MainPage()
    {
        this.InitializeComponent();
    }


    internal async void initializeCamera()
    {
        m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
        await m_mediaCaptureMgr.InitializeAsync();
        statusBox.Text = "initialized";
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    internal async void takePicture(object sender, RoutedEventArgs e)
    {

        m_photoStorageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
        ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
        await m_mediaCaptureMgr.CapturePhotoToStorageFileAsync(imageProperties, m_photoStorageFile);
    }

    private void initializeButton(object sender, RoutedEventArgs e)
    {
        initializeCamera();
    }
}

}

However, when I click on the initializeButton, I got an exception:

UnauthorizedAccessException (Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)))

What could be the problem here?

EDIT: I found the bug. Basically, if the webcam is already initialized, trying to initialize it again will trigger an exception. So I had to put in a flag, and some try/catch

Upvotes: 2

Views: 1339

Answers (2)

Mayank
Mayank

Reputation: 8852

Seect the Microphone and Webcam capability in Package.Appmnifest file

enter image description here

Upvotes: 0

ChristiaanV
ChristiaanV

Reputation: 5541

Have you set the capabilities for Microphone and Webcam in the manifest file?

Upvotes: 1

Related Questions