Question

I am copying all images from my device to directory. While copying the images I am getting this error Operation not permitted on IsolatedStorageFileStream.

Here is my code to copy the files.

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); 
        }
     }
  }

}

Here is my ConvertToBytes method.

    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;
        }
    }

Basically what I am trying is to create a zip file of all images. I have total 222 images in my device. So how can I solve this issue ? How can I create a zip of this images?

No correct solution

OTHER TIPS

Most probably this is due to the concurrent access to the file you can refer to the link: Operation not permitted on IsolatedStorageFileStream. error

I checked your code and it seems to be working (providing there's no error in DisposeImage() method) There is no OperationNotPermittedException occuring. However, if there is error in your code, then it can only be because of deleteFileFolder("ImagesZipFolder") line. Can you give me the snippet so that I can study it further. I m posting the working code... I have replaced that method with simple predefined one--

  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);
                }
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top