我试图将图像从网络保存到后台任务的独立存储中,但它抛出
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/