1

我正在将所有图像从我的设备复制到目录。复制图像时,出现此错误Operation not allowed on IsolatedStorageFileStream

这是我复制文件的代码。

MediaLibrary m = new MediaLibrary();
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
  if (store.DirectoryExists("ImagesZipFolder"))
  {
     deleteFileFolder("ImagesZipFolder");
  }

  if (!store.DirectoryExists("ImagesZipFolder"))
  {
     store.CreateDirectory("ImagesZipFolder");

     foreach (var picture in m.Pictures)
     {
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder/" + picture.Name, FileMode.CreateNew, store))
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(picture.GetImage());

            byte[] bytes = ConvertToBytes(image);
            stream.Write(bytes, 0, bytes.Length); 
        }
     }
  }

}

这是我的ConvertToBytes方法。

    public byte[] ConvertToBytes(BitmapImage bitmapImage)
    {
        byte[] data = null;
        WriteableBitmap wBitmap = null;

        using (MemoryStream stream = new MemoryStream())
        {
            wBitmap = new WriteableBitmap(bitmapImage);
            wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
            stream.Seek(0, SeekOrigin.Begin);
            //data = stream.GetBuffer();
            data = stream.ToArray();
            DisposeImage(bitmapImage);
            return data;
        }
    }

基本上我正在尝试创建一个包含所有图像的zip 文件。我的设备中共有222张图像。那么我该如何解决这个问题呢?如何创建此图像的 zip?

4

2 回答 2

0

我检查了您的代码,它似乎可以正常工作(前提是 DisposeImage() 方法中没有错误)没有发生 OperationNotPermittedException。但是,如果您的代码中有错误,则只能是因为 deleteFileFolder("ImagesZipFolder") 行。你能给我这个片段,以便我可以进一步研究它。我正在发布工作代码...我已用简单的预定义方法替换了该方法-

  MediaLibrary m = new MediaLibrary();
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.DirectoryExists("ImagesZipFolder"))
        {

            store.DeleteDirectory("ImagesZipFolder");
        }

        if (!store.DirectoryExists("ImagesZipFolder"))
        {
            store.CreateDirectory("ImagesZipFolder");

            foreach (var picture in m.Pictures)
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder/" + picture.Name, FileMode.CreateNew, store))
                {
                    BitmapImage image = new BitmapImage();
                    image.SetSource(picture.GetImage());

                    byte[] bytes = ConvertToBytes(image);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
        }
    }
于 2015-01-05T19:49:32.800 回答
0

这很可能是由于对文件的并发访问,您可以参考链接: IsolatedStorageFileStream 上不允许操作。错误

于 2014-11-10T04:06:17.717 回答