windows-phone-8 - 将图像保存到与 BG 任务隔离的存储中

标签 windows-phone-8 background-process isolatedstorage

我试图将图像从网络保存到后台任务的独立存储中,但它抛出

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll

我正在使用这段代码

 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            StreamResourceInfo sri = null;
            Uri uri = new Uri(tempJPEG, UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.Result);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }

当它从应用程序运行时,它可以 100% 工作,但不能从后台任务运行。 关于如何保存图像有什么提示吗?

最佳答案

您需要调用此代码 Dispatcher.BeginInvoke(),因为 WriteableBitmap 需要在 UI 线程而不是后台线程上执行。参见@http://codeblog.vurdalakov.net/2012/02/solution-wp7-unauthorizedaccessexceptio.html

关于windows-phone-8 - 将图像保存到与 BG 任务隔离的存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127266/

相关文章:

c# - 保留录制视频的最后 X 分钟

javascript - 是否可以在 css 中更改@viewport

python - stdin 从管道循环进程中读取 python

c# - 下载 mp3 并另存为铃声

c# - 在 Windows Phone 中播放独立存储中的媒体文件?

azure - 使用 REST API 向 Azure 服务总线队列或主题发送消息

c# - Windows Phone 8 消息框问题

后台 iOS 文字转语音

iphone - 如何在 swift 3 中为 iPhone 制作一个重复倒数计时器应用程序,即使在后台时也会每 'x' 分钟触发一次通知

silverlight-4.0 - 银光 4.0 : IsolatedStorage URI