WP 8.1 - 如果知道它的URL,如何将图像保存到隔离存储

时间:2015-06-20 17:40:50

标签: c# image windows-phone windows-phone-8.1 isolatedstorage

我有图片网址

如何在WP 8.1中保存到隔离存储

是否可以同时触发 保存 ,然后只需一个按钮即可 分享 进入Facebook?

这是我的代码 - 在BurakKaanKöse之后很有效:

public async void GetImage()
    {
        StorageFolder folder = ApplicationData.Current.LocalFolder;
            if (folder != null)
            {
                StorageFile file = await folder.CreateFileAsync("imagefile", CreationCollisionOption.ReplaceExisting);

                string url = imgUri[fLFl.SelectedIndex].ToString();
                HttpClient client = new HttpClient();
                byte[] fileContent = await client.GetByteArrayAsync(url); ; // This is where you set your content as byteArray

                Stream fileStream = await file.OpenStreamForWriteAsync();
                fileStream.Write(fileContent, 0, fileContent.Length);
                fileStream.Flush();
                fileStream.Dispose();
            }
    }

1 个答案:

答案 0 :(得分:0)

不要忘记更改'imagefile'路径和fileContent变量。

private async void SaveFile()
{
    try
    {
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        if(folder != null)
        {
            StorageFile file = await folder.CreateFileAsync("imagefile", CreationCollisionOption.ReplaceExisting);
            byte[] fileContnet = null; // This is where you set your content as byteArray
            Stream fileStream = await file.OpenStreamForWriteAsync();
            fileStream.Write(fileContent, 0, fileContent.Length);
            fileStream.Flush();
            fileStream.Close();
        }
    }
    catch (Exception ex)
    {
        // Some Exception handling code
    }
}