MediaLibrary.SavePicture 方法导致 System.UnauthorizedAccessException [英] MediaLibrary.SavePicture method results in a System.UnauthorizedAccessException

查看:21
本文介绍了MediaLibrary.SavePicture 方法导致 System.UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got the following code which handles downloading and saving an Image to the phone's media library. It fails with a System.UnauthorizedAccessException as if there was some cross-thread access. To my understading all code below an await statement runs on the UI thread so this should not be an issue. In addition I've tried wrapping the code below var stream = await client.OpenReadTaskAsync(this.Url); with Deployment.Current.Dispatcher.BeginInvoke but it did not help. :( I am running this on WP8 with the intention to port the code later to 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);
        }
    }

推荐答案

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

Did you add an ID_CAP_MEDIALIB_PHOTO capability to your app's manifest?

UnauthorizedAccessException 是 99% 的缺失功能.

UnauthorizedAccessException is 99% of the time a missing capability.

这篇关于MediaLibrary.SavePicture 方法导致 System.UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆