将图像保存到BG任务的隔离存储中

时间:2013-01-02 18:33:39

标签: 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%运行,但不能从后台任务运行。 有关如何保存图像的任何提示?

1 个答案:

答案 0 :(得分:3)

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