Windows phone 7 “例外”;不允许对隔离的存储文件流进行操作。”;

Windows phone 7 “例外”;不允许对隔离的存储文件流进行操作。”;,windows-phone-7,Windows Phone 7,我正在尝试从IsolatedStorage保存图像。我遇到一个错误:“不允许对IsolatedStorage FileStream执行操作。”。我的代码如下所示。我怎样才能克服这个问题 public HomePage() { InitializeComponent(); // Create a filename for JPEG file in isolated storage. String tempJ

我正在尝试从IsolatedStorage保存图像。我遇到一个错误:“不允许对IsolatedStorage FileStream执行操作。”。我的代码如下所示。我怎样才能克服这个问题

  public HomePage()
        {
            InitializeComponent();
            // Create a filename for JPEG file in isolated storage.
            String tempJPEG = "/Images/homescreenmap.png";

            // Create virtual store and file stream. Check for duplicate tempJPEG files.
            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(sri.Stream);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

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

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


        }

IsolatedStorageException意味着它无法找到您设置的位置的路径,在您的情况下,这是文件夹图像。创建文件前只需添加以下代码:

if (!myIsolatedStorage.DirectoryExists("Images"))
{
    myIsolatedStorage.CreateDirectory("Images");
}

:感谢它的工作,但新的NullReffrenceException正在生成。在哪行代码中获得NullReferenceException?我猜它可能在这一行“bitmap.SetSource(sri.Stream);”,因为“sri=Application.GetResourceStream(uri);”返回null。