【问题标题】:MediaLibrary.SavePicture method results in a System.UnauthorizedAccessExceptionMediaLibrary.SavePicture 方法导致 System.UnauthorizedAccessException
【发布时间】:2013-01-18 06:15:49
【问题描述】:

我有以下代码处理下载图像并将其保存到手机的媒体库。它以System.UnauthorizedAccessException 失败,就好像有一些跨线程访问一样。据我了解,await 语句下方的所有代码都在 UI 线程上运行,因此这应该不是问题。此外,我尝试用Deployment.Current.Dispatcher.BeginInvoke 包装var stream = await client.OpenReadTaskAsync(this.Url); 下面的代码,但它没有帮助。 :( 我在 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);
        }
    }

【问题讨论】:

    标签: windows-phone-7 windows-phone-8


    【解决方案1】:

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

    UnauthorizedAccessException 有 99% 的情况是缺少功能。

    【讨论】:

    • 谢谢!添加功能修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多