如何从网址下载图像并将其保存到本地SQLite数据库 [英] How to Download Image From Url and Save It to a Local SQLite Database
问题描述
在Xamarin中,如何从URL下载图像?
In Xamarin, how do you download an image from a URL?
然后,如何将图像保存到设备上的本地SQLite数据库中?
And then, how do you save the image to a local SQLite database on the device?
我的Xamarin.Forms应用当前遵循 Xamarin文档将URL用作Image
的ImageSource
属性,这很好用.但是,我注意到,每次启动该应用程序时,它都会通过网络重新下载该映像.我希望从URL下载一次图像,然后将其本地保存到设备中.此方法将为用户节省电池和数据使用量.
My Xamarin.Forms app currently follows the Xamarin Documentation to use a URL for the ImageSource
property of an Image
, and this works fine. But, I've noticed that every time the app launches, it re-downloads this image over the network. I'd prefer to download the image from the URL once and save it locally to the device; this method will save battery and data usage for the user.
推荐答案
说明
要完成此操作,我们将使用HttpClient
从网址中以byte[]
的形式下载图像,然后将其保存到我们的本地SQLite数据库中.
Explanation
To accomplish this, we'll download the image from the Url as a byte[]
using HttpClient
, then save it to our local SQLite database.
此处是一个示例应用程序,它使用Xamarin.Forms完成了此任务.为了获得最佳理解,我建议从GitHub下载代码.
Here is a sample app that accomplishes this using Xamarin.Forms. For the best understanding, I recommend downloading the code from GitHub.
我们将使用HttpClient
将图像下载为byte[]
.这样可以更轻松地将其存储在我们的SQLite数据库中.
We will download the image as a byte[]
using HttpClient
. This will make it easier to store it in our SQLite Database.
const int _downloadImageTimeoutInSeconds = 15;
readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(_downloadImageTimeoutInSeconds) };
async Task<byte[]> DownloadImageAsync(string imageUrl)
{
try
{
using (var httpResponse = await _httpClient.GetAsync(imageUrl))
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
return await httpResponse.Content.ReadAsByteArrayAsync();
}
else
{
//Url is Invalid
return null;
}
}
}
catch (Exception e)
{
//Handle Exception
return null;
}
}
将模型保存到数据库
将图像下载为byte[]
后,我们会将其存储在SQLite数据库中.
Save Model to Database
Once you've downloaded the image as a byte[]
, we'll store it in the SQLite database.
我将在下一部分中添加有关该模型的更多信息.
I've added more information about the model in the next section.
public static async Task SaveDownloadedImage(DownloadedImageModel downloadedImage)
{
var databaseConnection = await GetDatabaseConnectionAsync();
await databaseConnection.InsertOrReplaceAsync(downloadedImage);
}
型号
在模型中,我创建了一个将图像存储为字符串的属性和一个将Image作为Xamarin.Forms.ImageSource
返回的只读属性.
Model
In the model, I've created a property that stores the image as a string and a read-only property that returns the Image as a Xamarin.Forms.ImageSource
.
public class DownloadedImageModel
{
[PrimaryKey]
public string ImageUrl { get; set;}
public byte[] DownloadedImageBlob { get; set; }
public ImageSource DownloadedImageAsImageStreamFromBase64String
{
get
{
try
{
if (DownloadedImageBlob == null)
return null;
var imageByteArray = DownloadedImageBlob;
return ImageSource.FromStream(() => new MemoryStream(imageByteArray));
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
}
}
这篇关于如何从网址下载图像并将其保存到本地SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!