How to Use IsolatedStorage to Set Lock Screen Background in Windows Phone 8

0

I would like to be able to use an image from my IsolatedStorage to modify the lock screen background, but I am having trouble getting the correct syntax of the IsolatedStorage file path to set the lock screen background.

In following http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx , I am calling the LockHelper method in a button click event once an image has been selected from a List named Recent (which has been populated from PictureRepository.cs which loads images from IsolatedStorage)

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        capturedPicture = (sender as LongListSelector).SelectedItem as CapturedPicture;

        if (capturedPicture != null)
        {
            //filename is the name of the image in IsolatedStorage
            fileName = capturedPicture.FileName;
        }
    }

void setAsLockScreenMenuItem_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(fileName)) 
        {
            //PictureRepository.IsolatedStoragePath is a string = "Pictures"                
            LockHelper("isostore:/" + PictureRepository.IsolatedStoragePath + "/" + fileName, false);  //results in FileNotFoundException
            LockHelper(PictureRepository.IsolatedStoragePath + "/" + fileName, false);  //results in ArgumentException
        }
        else
        {
            MessageBoxResult result = MessageBox.Show("You must select an image to set it as your lock screen.", "Notice", MessageBoxButton.OK);
            if (result == MessageBoxResult.OK)
            {
                return;
            }
        }
    }

Once LockHelper is called, the event proceeds

private async void LockHelper(string filePathOfTheImage, bool isAppResource)
    {
        try
        {
            var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
            if (!isProvider)
            {
                // If you're not the provider, this call will prompt the user for permission.
                // Calling RequestAccessAsync from a background agent is not allowed.
                var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();

                // Only do further work if the access was granted.
                isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
            }

            if (isProvider)
            {
                // At this stage, the app is the active lock screen background provider.

                // The following code example shows the new URI schema.
                // ms-appdata points to the root of the local app data folder.
                // ms-appx points to the Local app install folder, to reference resources bundled in the XAP package.
                var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
                var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);

                //The Error Occurs Here!
                // Set the lock screen background image.
                Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);

                // Get the URI of the lock screen background image.
                var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri();
                System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Cannot update your lock screen background at this time.", "Notice", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    return;
                }
            }
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

The error occurs on Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); within the LockHelper method. It is either a FileNotFoundException mentioning The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B) or an ArgumentException. Everywhere I've referenced says for using IsolatedStorage the Uri should be ms-appdata:///local/ and then the IsolatedStorage file path (in my case Pictures/imagename.jpg), and I believe my error lies with the Uri path, but I am unsure of the correct syntax? If it is not this, any other ideas?

c#
windows-phone-8
isolatedstorage
lockscreen
asked on Stack Overflow Aug 20, 2013 by Matthew • edited Aug 20, 2013 by Matthew

2 Answers

0

Try this at nokia developer. Hope you'll get the way to the solution.

answered on Stack Overflow Aug 20, 2013 by Amit Singh
0

This is what worked for me:

const string filePathOfTheImage = "/Shared/ShellContent/shot2.jpg"; //This is where my image is in isostore

var uri = new Uri("ms-appdata:///local" + filePathOfTheImage, UriKind.Absolute);

Also, I use WP Power Tools for tracking my app storage space. Hope this helps.

answered on Stack Overflow Feb 21, 2014 by sid

User contributions licensed under CC BY-SA 3.0