gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:30 25 4
gpt4 key购买 nike

我正在编写一个 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);

希望这对遇到和我一样问题的人有帮助!

最佳答案

来自@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);

关于c# - 无法使用从 skydrive 获取的文件流来创建 BitmapImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18423586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com