简体   繁体   中英

Can't use file stream obtained from skydrive to create BitmapImage

I'm writing a windows phone 8 app and I would like to let users use images saved to their skydrive in my app. The piece of code I'm having trouble with (I believe) is below.

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);

all the signing in and authentication stuff that the user needs to do is already done by this point and works as expected. When my program reaches the marked line, it suddenly crashes. The errors I receive are...

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

Does anyone know how to fix this?

I inserted breakpoints to trace the program and it appears that the storage file is being made and the uri is correct, but when I try to download the file to it, the program gives me the error. I confirmed the filepath for the file on skydrive is also correct. If I try to use DownloadAsync() instead it appears to work but then the program crashes when I try to use the stream obtained from the skydrive file instead and gives the same error.

Any ideas? Because I can't figure out what could be wrong.

The file being downloaded is a png image.

EDIT: Solution Found

After some more research I found out that when you download files from skydrive a call for a files id...

filepath = result.id;

as above that does not give you the contents of the file. I didn't check what it was obtaining but I'd assume it was probably the metadata. To obtain the actual contents of the file you must add "/contents".

The correct path would then be

filepath = result.id + "/contents";

I edited my code as shown below and it now works perfectly.

        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);

Hope this helps anyone having the same problem as I was!

From the @deboxturtle's update, as an answer for search sake:

After some more research I found out that when you download files from skydrive a call for a files id...

filepath = result.id;

as above that does not give you the contents of the file, you get file metadata as json. To obtain the actual contents of the file you must add /content to the id.

The correct path would then be

filepath = result.id + "/content";

I edited my code as shown below and it now works perfectly.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM