如何将图像数据流保存到 Windows Phone 中本地应用程序数据文件夹的根目录?

问题描述:

我正在尝试将图像数据流保存到文件中.不过,我能够将其保存到图片库中.但我想将它保存到我的应用程序/项目根目录中的一个文件中.我正在尝试以下但它不起作用.

I am trying to save the stream of image data to a file. I was able to save it to Pictures library though. But I want to save it to a file in the root of my application/ project. I was trying the below but it doesn't work.

         using (MediaLibrary mediaLibrary = new MediaLibrary())
         mediaLibrary.SavePicture(@"\DefaultScreen.jpg", stream);

在这种情况下,您应该使用 LocalStorage.这是一个简单的解决方案:

In this case you should use LocalStorage. Here is a simple solution to do this:

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
  if (!isoStore.FileExists(fileName)
  {
    var sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

    using (var br = new BinaryReader(sr.Stream))
    {
      byte[] data = br.ReadBytes((int)sr.Stream.Length);
      string strBaseDir = string.Empty;
      const string DelimStr = "/";
      char[] delimiter = DelimStr.ToCharArray();
      string[] dirsPath = fileName.Split(delimiter);

      // Recreate the directory structure
      for (int i = 0; i < dirsPath.Length - 1; i++)
      {
          strBaseDir = Path.Combine(strBaseDir, dirsPath[i]);
          isoStore.CreateDirectory(strBaseDir);
      }

      using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
      {
          bw.Write(data);
      }
    }
  }
}

您可以在此处找到有关 Windows Phone 中数据的所有信息:

Here you can find all info about data in Windows Phone:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402541(v=vs.105).aspx