Método MediaLibrary.SavePicture resulta em um System.UnauthorizedAccessException
Eu tenho o seguinte código que lida com o download e salvando uma imagem na biblioteca de mídia do telefone. Ele falha com umSystem.UnauthorizedAccessException
como se houvesse algum acesso cross-thread. Para meu entendimento de todo o código abaixo, uma instrução await é executada no thread da interface do usuário, portanto, isso não deve ser um problema. Além disso, eu tentei envolver o código abaixovar stream = await client.OpenReadTaskAsync(this.Url);
comDeployment.Current.Dispatcher.BeginInvoke
mas isso não ajudou. :( Estou executando isso no WP8 com a intenção de portar o código mais tarde para o WP7.
private async void OnSaveImageCommand()
{
RunProgressIndicator(true, "Downloading image...");
var client = new WebClient();
try
{
var stream = await client.OpenReadTaskAsync(this.Url);
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
using (var memoryStream = new MemoryStream())
{
var writeableBitmap = new WriteableBitmap(bitmap);
writeableBitmap.SaveJpeg(memoryStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0,
100);
memoryStream.SetLength(memoryStream.Position);
memoryStream.Seek(0, SeekOrigin.Begin);
var mediaLibrary = new MediaLibrary();
mediaLibrary.SavePicture("image.jpg", memoryStream);
MessageBox.Show("Image has been saved to the phone's photo album");
}
}
catch
{
MessageBox.Show("Failed to download image");
}
finally
{
RunProgressIndicator(false);
}
}