When generating a lock screen image for Windows Phone 8.0 silverlight app, I use the following code:

fileName = Guid.NewGuid() + ".jpg";

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{                            
    using (var isostream = iso.CreateFile(fileName))
    {
        Extensions.SaveJpeg(bmp, isostream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
        isostream.Close();
    }
}

This works absolutely fine both when I run the app or via the background task (assuming the lock screen setting is on).

I've recently added code that runs whenever I launch the app to delete previous lock screen images for storage efficiency purposes.

This is the code which is used in App.xaml.cs:

using (var iso = IsolatedStorageFile.GetUserStoreForApplication() )
{
    foreach (string file in GetAllFiles("*.jpg", iso))
    {
        iso.DeleteFile(file);
    }

    iso.Dispose();
}

The GetAllFiles method was copied from: http://msdn.microsoft.com/en-us/library/zd5e2z84%28v=vs.110%29.aspx.

When debugging this works fine; all lock screen images that were created since the last time the app launched would get successfully deleted. Just to be clear, the only jpg's stored by the app are the lock screen images.

However, the problem is whenever I do include this delete operation, the lock screen image stops updating from that moment onwards. I get this exception whenever any attempt is made to update it:

System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream. at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf) at System.IO.IsolatedStorage.IsolatedStorageFile.OpenFile(String path, FileMode mode) ...

A few things to keep in mind:

1) whenever the app is launched, the delete operation is run first in App.xaml.cs, whereas the lock screen image gets updated at a later point by the view model (this is for a weather app, so the new lock screen image is created after the weather is returned).

2) the delete operation is only run when the app is actively opened AND the lock screen setting is on; it isn't called at all in the background task.

What is weird is when I do a fresh install of the app, the lock screen setting by default is off. When I turn the lock screen on, the lock screen image gets created successfully. If I close the app and never relaunch it (thereby never calling the delete operation code), the lock screen image never gets updated because the background task runs into the same previously referenced exception.

If I follow the same exact steps but with no delete operation code in App.xaml.cs, the lock screen image gets updated successfully every time.

3) the live tile continues to be updated without any problems, both when the app is launched or in the background task (that it shares with the lock screen).

Any ideas what's going on here, and what I can do to address this?

2

There are 2 best solutions below

7
Rahul Ranjan On

can you please provide the code snippet for getting image from isolated storage and updating it to lock screen

Usually this exception is thrown when two threads are trying to access the same isolated storage. This can occur only when it is not attached to debugger (in your case). It is tricky to find. I think while the images are deleted, in same moment the image is also getting saved.

0
Shawn Kendrot On

The documentation suggests just alternating between two file names like such:

string fileName;
var currentImage = LockScreen.GetImageUri();

if (currentImage.ToString().EndsWith("_A.jpg"))
{
    fileName = "LiveLockBackground_B.jpg";
}
else
{
    fileName = "LiveLockBackground_A.jpg";
}
var lockImage = string.Format("{0}", fileName);

I use this approach in one of my apps without issue.

If you wish to continue to use the random Guid, then you should check that the file name is not the same as the current lockscreen before trying to delete.

var currentImage = LockScreen.GetImageUri();
using (var iso = IsolatedStorageFile.GetUserStoreForApplication() )
{
    foreach (string file in GetAllFiles("*.jpg", iso))
    {
        if(AreFilesTheSame(currentImage, file) == false)
        {
            iso.DeleteFile(file);
        }
    }
}