Operation not permitted on IsolatedStorageFileStream error

Re: Anyone familiar with isolated storage?

I have some background images in my app that I retrieve from URLs and cache in isolated storage so maybe this will be helpful for you:
Code:
private static async void image_ImageOpened(object sender, RoutedEventArgs e)
{
    var image = sender as BitmapImage;
    if (image == null) return;
    string fileName = GetFileNameFromPath(image.UriSource.AbsolutePath);
    var writableBitmap = new WriteableBitmap(image);
    var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(AppConstants.LOCALFOLDERNAME_BACKGROUND, CreationCollisionOption.OpenIfExists);
    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    using (var stream = await file.OpenStreamForWriteAsync())
        Extensions.SaveJpeg(writableBitmap, stream, image.PixelWidth, image.PixelHeight, 0, 100);
}

private static string GetFileNameFromPath(string path)
{
    int lastSlashIndex = path.LastIndexOf('/');
    return HttpUtility.UrlDecode(path.Substring(lastSlashIndex + 1, path.Length - lastSlashIndex - 1));
}

Btw: 'async void' is bad but I wanted to run this code in an event handler so I'm not sure there's a better way
 
Last edited:
Re: Anyone familiar with isolated storage?

I got some assistance and have got something partially working. There is one issue and one wish. First the issue. The way it works right now is. First time using app, user presses button. Sound downloads in background from url. Press button again, sound plays. However, if you press the same button again, I get an Operation not permitted on IsolatedStorageFileStream at this line: audioStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open);


That tells me it goes through the code fine to download it with webclient. Then even plays it. But to play the same button sound again, it errors. However, if I dont press the same button again, and press another one, I can go back to the initial one and play it. Just never can play it twice in a row without the app crashing. Here is the relevant code:


Code:
 private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LongListSelector selector = sender as LongListSelector;

            // verifying our sender is actually a LongListSelector
            if (selector == null)
                return;

            SoundData data = selector.SelectedItem as SoundData;
            
            // verifying our sender is actually SoundData
            if (data == null)
                return;

            if (data.IsDownloaded)
            {
                if (audioStream != null)
                {
                    audioStream.Close();
                    
                }

                audioStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open);

                // Apparently MediaElement doesn't like isostore:/
                AudioPlayer.SetSource(audioStream);
                AudioPlayer.Play();
                
            }

            else
            {
              
                WebClient client = new WebClient();
                client.OpenReadCompleted += (senderClient, args) =>
                    {
                        using (IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(data.SavePath))
                        {
                            args.Result.Seek(0, SeekOrigin.Begin);
                            args.Result.CopyTo(fileStream);
                        }
                    };
                client.OpenReadAsync(new Uri(data.FilePath));
            }

            selector.SelectedItem = null;



Another thing is it doesn't play the sound after downloading without pressing the button again. I was trying to get that to work right but couldnt. But obviously my main concern is the crashing.
 
Try step-by-step debuging and you'll see what line is raising exception and what exception would it be.
 

Members online

Forum statistics

Threads
335,587
Messages
2,258,591
Members
428,746
Latest member
kioonchool