无法使用从skydrive获取的文件流来创建BitmapImage

时间:2013-08-24 22:05:58

标签: c# .net windows-phone-8

我正在写一个Windows Phone 8应用程序,我想让用户在我的应用程序中使用保存到他们的skydrive的图像。我遇到问题的代码(我相信)在下面。

StorageFile thefile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("b4b.png", CreationCollisionOption.ReplaceExisting);
        Uri theuri = new Uri("ms-appdata:///local/b4b.png");
        var thething = await client.BackgroundDownloadAsync(filepath, theuri); <--- line where program crashes
        BitmapImage src = new BitmapImage();
        src.SetSource((Stream)await thefile.OpenReadAsync());
        WriteableBitmap image = new WriteableBitmap(src);

用户需要执行的所有登录和身份验证功能已经完成,并按预期工作。当我的程序到达标记的行时,它突然崩溃。我收到的错误是......

A first chance exception of type 'System.ArgumentException' occurred in Microsoft.Live.DLL
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded        'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll

有谁知道如何解决这个问题?

我插入断点来跟踪程序,看起来正在创建存储文件并且uri是正确的,但是当我尝试将文件下载到它时,程序会给我错误。我确认skydrive上文件的文件路径也是正确的。如果我尝试使用DownloadAsync()而不是它似乎工作,但是当我尝试使用从skydrive文件获得的流时程序崩溃并给出相同的错误。

有什么想法吗?因为我无法弄清楚可能出现的问题。

正在下载的文件是png图像。

编辑:找到解决方案

经过一些研究后,我发现当你从skydrive下载文件时,需要调用文件ID ......

filepath = result.id;

如上所述,它不会提供文件的内容。我没有检查它是什么,但我认为它可能是元数据。要获取文件的实际内容,您必须添加“/ contents”。

正确的路径是

filepath = result.id + "/contents";

我编辑了我的代码,如下所示,它现在完美无缺。

        StorageFile thefile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("b4b.png", CreationCollisionOption.ReplaceExisting);
        Uri theuri = new Uri("ms-appdata:///local/b4b.png", UriKind.Absolute);
        var thething = await client.DownloadAsync(filepath + "/content");
        Stream stream = thething.Stream;
        stream.Seek(0, SeekOrigin.Begin);
        BitmapImage src = new BitmapImage();
        src.SetSource(stream);

希望这有助于任何人遇到与我一样的问题!

1 个答案:

答案 0 :(得分:0)

从@deboxturtle的更新中,作为搜索的答案:

经过一些研究后,我发现当你从skydrive下载文件时,需要调用文件ID ......

filepath = result.id;

如上所述没有提供文件的内容,您将获得文件元数据为json。要获取文件的实际内容,您必须将/content添加到ID。

正确的路径是

filepath = result.id + "/content";

我编辑了我的代码,如下所示,它现在完美无缺。

var filepath = result.id + "/content";
StorageFile thefile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                              "b4b.png", CreationCollisionOption.ReplaceExisting);
var thething = await client.DownloadAsync(filepath);
Stream stream = thething.Stream;
stream.Seek(0, SeekOrigin.Begin);
BitmapImage src = new BitmapImage();
src.SetSource(stream);