7

I'm trying to set a remote image as the desktop wallpaper / phone lockscreen in my W10 UWP app:

string name = "test_image.jpg";
Uri uri = new Uri("http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg");

// download image from uri into temp storagefile
var file = await StorageFile.CreateStreamedFileFromUriAsync(name, uri, RandomAccessStreamReference.CreateFromUri(uri));

// file is readonly, copy to a new location to remove restrictions
var file2 = await file.CopyAsync(KnownFolders.PicturesLibrary);

// test -- WORKS!
//var file3 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Design/1.jpg"));

// try set lockscreen/wallpaper
if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) // Phone
    success = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(file2);
else // PC
    success = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file2);

file1 doesn't work as it is read-only, so I copy it to a new location (pictures library) to remove restrictions -> file2.

Note: file3 works, so I'm not sure what's happening -- I assume TrySetWallpaperImageAsync/TrySetLockScreenImageAsync only accepts msappx local files...

Anyone have any ideas on work arounds?

Thanks.

Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
Travis Liew
  • 787
  • 1
  • 11
  • 34
  • 3
    Have you tried saving the target file into ApplicationData.Current.LocalFolder? Apparently these methods only accept files from specific folders. – Martin Suchan Aug 08 '15 at 18:07
  • Ah, that was it! Thanks. – Travis Liew Aug 09 '15 at 00:04
  • I would also suggest to check if your device family is Mobile rather than checking for API, while there isn't any difference now in the future it might not work properly depending on what device families do appear. – Ivan Ičin Apr 07 '16 at 12:39

1 Answers1

5

Save your remote image to ApplicationData.Current.LocalFolder first, then use TrySetWallpaperImageAsync/TrySetLockScreenImageAsync and point to the saved image instead of directly referencing the remote image should work.

Nik A.
  • 454
  • 6
  • 15