W10 UWP - Set remote image as desktop wallpaper / lockscreen
Asked Answered
D

1

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.

Dystopia answered 6/8, 2015 at 14:4 Comment(3)
Have you tried saving the target file into ApplicationData.Current.LocalFolder? Apparently these methods only accept files from specific folders.Angary
Ah, that was it! Thanks.Dystopia
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.Retha
R
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.

Revelationist answered 26/8, 2015 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.