MediaLibrary.SavePicture方法导致System.UnauthorizedAccessException

zya*_*ash 0 windows-phone-7 windows-phone-8

我有以下代码处理下载并将图像保存到手机的媒体库.它失败了System.UnauthorizedAccessException,好像有一些跨线程访问.为了我的理解,await语句下面的所有代码都在UI线程上运行,所以这应该不是问题.另外我试过包装下面的代码var stream = await client.OpenReadTaskAsync(this.Url);,Deployment.Current.Dispatcher.BeginInvoke但它没有帮助.:(我在WP8上运行此程序,打算稍后将代码移植到WP7.

    private async void OnSaveImageCommand()
    {
        RunProgressIndicator(true, "Downloading image...");
        var client = new WebClient();
        try
        {
            var stream = await client.OpenReadTaskAsync(this.Url); 

            var bitmap = new BitmapImage();
            bitmap.SetSource(stream);

            using (var memoryStream = new MemoryStream())
            {
                var writeableBitmap = new WriteableBitmap(bitmap);
                writeableBitmap.SaveJpeg(memoryStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0,
                                         100);
                memoryStream.SetLength(memoryStream.Position);
                memoryStream.Seek(0, SeekOrigin.Begin);

                var mediaLibrary = new MediaLibrary(); 
                mediaLibrary.SavePicture("image.jpg", memoryStream);
                MessageBox.Show("Image has been saved to the phone's photo album");
            }
        }
        catch 
        {
            MessageBox.Show("Failed to download image"); 
        }
        finally
        {
            RunProgressIndicator(false);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Jus*_*gel 8

您是否在应用的清单中添加了ID_CAP_MEDIALIB_PHOTO功能?

ID_CAP_MEDIALIB_PHOTO

UnauthorizedAccessException是99%的缺失功能.