winnie1pooh
winnie1pooh

Reputation: 13

LockScreen.SetImageFileAsync(...) causes UnauthorizedAccessException

I'm trying to set background image on lock screen in my WinRT app. But when this code is being executed i get an UnauthorizedAccessException with message:

"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

The code was taken from MSDN and looks like OK.

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var imagePicker = new FileOpenPicker
        {
            ViewMode = PickerViewMode.Thumbnail,
            SuggestedStartLocation = PickerLocationId.PicturesLibrary,
            FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" },
        };

        var imageFile = await imagePicker.PickSingleFileAsync();

        if (imageFile != null)
        {
            await LockScreen.SetImageFileAsync(imageFile);
        }
    }

The exception described below is thrown in this line of code:

await LockScreen.SetImageFileAsync(imageFile);

By the way, i've tried to install some applications which can change your background on lock screen, but all of them show error or just crash. Maybe something is wrong with my OS version?

Does anyone know how to solve this problem? Please help!

Upvotes: 0

Views: 813

Answers (3)

PolarBear
PolarBear

Reputation: 203

I faced with exactly the same problem. The problem was that my OS wasn't activated. Check this thing on your computer's properties. Hope it helps.

Upvotes: 0

Anobik
Anobik

Reputation: 4899

I guess Its some kind of privilege problem may be admin .

Try a work around by applying this code

private async void Button_Click(object sender, RoutedEventArgs e)
    {
        var client = new HttpClient();
        var bytes = await client.GetByteArrayAsync(new Uri("http://transfer-talk.com/wp-content/uploads/Kaka-Real-Madrid.jpg"));
        StorageFile sf = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("test.jpg", CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteBytesAsync(sf, bytes);
        //var imageFile = await imagePicker.PickSingleFileAsync();

        //if (imageFile != null)
        {
            await LockScreen.SetImageFileAsync(sf);
        }
    }

it will download an Image and set . Not giving an exception in my case neither your code nor mine.

download this sample and try running and see if error exist lock screen sample

also try setting the stream rather using storage file.

await LockScreen.SetImageStreamAsync(await sf.OpenReadAsync());

try and let me know :)

Upvotes: 0

AndreasW
AndreasW

Reputation: 1045

You need access to the Pictures Library. Set it by opening your Package.appxmanifest, goto Capabilities and check Pictures Library.

Upvotes: 1

Related Questions