title: How to use LockScreen.SetImageUri to Set Windows Phone 8 Lock Screen Background
body: <p>I have an image in IsolatedStorage, and I would like to programmatically set it as the device lock screen background. My problem is that I cannot get the correct path required by <code>LockScreen.SetImageUri</code>. From referencing <a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx</a> it is evident that `"ms-appdata:///local/" is the required precursor for local images. </p>
  
  <pre><code>var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
  var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);
  </code></pre>
  
  <p>I have created a folder in my applications IsolatedStorage called <code>Pictures</code> in which jpg images are saved from the CameraCaptureTask. I have tried several ways to access images within this folder via the above scheme but I always receive an <code>ArgumentException</code> on the next line </p>
  
  <pre><code>Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);
  </code></pre>
  
  <p>When debugging, however, I see that <code>uri = "ms-appdata:///Local/Pictures/WP_20130812_001.jpg"</code>, how is this not correct?</p>
  
  <p>My implementation is as follows</p>
  
  <pre><code>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 the IsolatedStorage folder named Pictures
          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;
          }
      }
  }
  
  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);
  
          // 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
      {
          MessageBox.Show("You said no, so I can't update your background.");
      }
  }
  catch (System.Exception ex)
  {
      System.Diagnostics.Debug.WriteLine(ex.ToString());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How might I modify <code>LockScreen.SetImageUri</code> to the proper expected uri?</p>