OneDrive下载* .jpeg图像文件在Windows Phone 8中损坏

时间:2014-04-21 15:36:14

标签: c# windows-phone-8 onedrive

我正在将我的应用数据库备份到OneDrive。数据库记录是指存储在隔离存储中的图像。我也备份这些图像。数据库文件。

备份文件的目的地是:

me/skydrive/my_documents/MyCompany/MyApp/MyBackup.bak

jpg图像文件的目的地是

me/skydrive/my_documents/MyCompany/MyApp/MyBackup Images/*.jpg

数据库恢复正常,但图像没有。我已经确认图像在SkyDrive上正确备份 - 我可以看到它并从SkyDrive打开它。但是,当我还原时,该文件已损坏。这是我用来恢复的代码:

dynamic cmpFolder = await oneDrive.FindFolder("MyCompany", "me/skydrive/my_documents");
dynamic appFolder = await oneDrive.FindFolder(AppName, cmpFolder.id);

string imagesFileName = Path.GetFileNameWithoutExtension(selectedFile.FileName) + " Images";
dynamic imgFolder = await oneDrive.FindFolder(imagesFileName, appFolder.id);

dynamic fileList = await oneDrive.FindFiles(imgFolder.id);

foreach (var fileData in fileList.data)
{
    string fileName = fileData.name;
    var file =
        await wilFolder.CreateFileAsync(
            Path.GetFileName(fileName), CreationCollisionOption.ReplaceExisting);

    var result = await client.BackgroundDownloadAsync(selectedFile.FileID + 
        "/content/", new Uri(@"\shared\transfers\" + fileName, UriKind.Relative));
}

使用ISETool和查看\ shared \ transfers,我可以看到该文件不再可读。它的大小约为128k,而原始图像要大得多。

我也试过这个,这是我的原始代码,直到我开始看到问题:

var downloadResult = await client.DownloadAsync(selectedFile.FileID + "/content/");

using (Stream oneDriveStream = downloadResult.Stream)
{ 
    oneDriveStream.Position = 0;
    byte[] imageBytes = new byte[oneDriveStream.Length];
    int count = oneDriveStream.Read(imageBytes, 0, imageBytes.Length);

    using (var s = await file.OpenStreamForWriteAsync())
    {
        oneDriveStream.CopyTo(s);

        // and tried this
        //s.Write(imageBytes, 0, imageBytes.Length);
    }
}

供参考,这里是FindFolder和FindFiles实现:

public async Task<dynamic> FindFiles(string folderName)
{
    LiveOperationResult filesResult = await client.GetAsync(folderName + "/files");
    dynamic files = filesResult.Result;

    return files;
}

public async Task<dynamic> FindFolder(string folderName, string parentFolder)
{
    LiveOperationResult folderResult = await client.GetAsync(parentFolder + "/files?filter=folders");
    dynamic folders = folderResult.Result;

    foreach (var folder in folders.data)
        if (folder.name == folderName)
            return folder;

    return null;
}

如何从OneDrive文件夹成功下载* .jpg图像?

1 个答案:

答案 0 :(得分:1)

请尝试使用以下路径:

var downloadResult = await client.DownloadAsync(selectedFile.FileID + "/picture?type=full");