如何在windows phone localstorage中保存httpclient下载的图像(jpeg)?

时间:2015-03-29 12:01:19

标签: windows-phone-8.1 winrt-xaml win-universal-app

我正在寻找将从httpClient下载的图像保存到本地存储的任何示例。我知道如何将文件从本地资源保存到本地存储。但是没有找到任何关于如何从uri下载后保存图像的例子?

现在我已达到目前

try
        {
            if (string.IsNullOrEmpty(imageUrl))
                return;
            var url = new Uri(imageUrl);
            var thumbnail = RandomAccessStreamReference.CreateFromUri(url);

            var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(Constants.Profile_Picture_Name, CreationCollisionOption.ReplaceExisting);

            //How to save RandomAccessStream to file here!
        }
        catch
        {

        }

1 个答案:

答案 0 :(得分:1)

得到了解决方案

 var url = new Uri(httpUrl);
            var fileName = Path.GetFileName(url.LocalPath);
            if (folder == null)
                folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(Constants.Member_Images_Folder, CreationCollisionOption.OpenIfExists);    

            HttpClient client = new HttpClient();
            var bytes = await client.GetByteArrayAsync(url);

            var imageFile = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBytesAsync(imageFile, bytes);