网络摄像头应用程序中的UnauthorizedAccessException

时间:2012-11-13 06:36:54

标签: windows-8 microsoft-metro

我正在尝试制作一个简单的网络摄像头应用程序:

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();
    }
}

}

然而,当我点击initializeButton时,我得到了一个例外:

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

这可能是什么问题?

编辑:我发现了这个错误。基本上,如果网络摄像头已经初始化,尝试再次初始化它将触发异常。所以我不得不放一个标志,一些尝试/捕捉

2 个答案:

答案 0 :(得分:1)

您是否在清单文件中设置了麦克风和网络摄像头的功能?

答案 1 :(得分:0)

Package.Appmnifest文件

中选择麦克风和网络摄像头功能

enter image description here