WP 8.1 - 如果知道图像是 URL,如何将图像保存到独立存储
本文关键字:图像 保存 存储 独立 如果 WP URL | 更新日期: 2023-09-27 17:57:11
我有图像网址。
如何将其保存到WP 8.1中的独立存储中。
我可以触发保存它,然后只用一个按钮分享到Facebook吗?
这是我的代码 - 遵循Burak Kaan Kö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();
}
}
不要忘记更改"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
}
}