MediaLibrary.SavePicture方法导致System.UnauthorizedAccessException

时间:2013-01-17 20:10:03

标签: 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);
        }
    }

1 个答案:

答案 0 :(得分:8)

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

ID_CAP_MEDIALIB_PHOTO

UnauthorizedAccessException是99%的缺失功能。