今天我正在處理如何將字節數組保存到SQLite數據庫的問題。當我將base []保存到數據庫時,它看起來像是保存該數組的屬性,但實際上它不是保存到數據庫,當我在設備上重新啓動應用程序時。也許我必須在將數據放入數據庫之前將byte []轉換爲base 64?如果是的話,我該怎麼做?也可能有另一種方式保存到sqlite數據庫,而不轉換爲base64?Xamarin格式,如何將byte []保存到SQLite數據庫?
這是我的對象類:
public class Unit
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public bool IsStarted { get; set; }
public byte[] CImage { get; set; }
}
在這裏,我選擇從畫廊的形象,並設置爲可綁定值:
IGalleryImageService galleryService = Xamarin.Forms.DependencyService.Get<IGalleryImageService>();
galleryService.ImageSelected += (o, imageSourceEventArgs) =>
{
ActiveP.CImageBindable = imageSourceEventArgs.ImageSource;
MemoryStream st = new MemoryStream(imageSourceEventArgs.ImageSource);
ImageSource imgSource = ImageSource.FromStream(() => st);
(ActiveP.Page as PTemplate).CImage.Source = imgSource;
};
galleryService.SelectImage();
這裏是我的其他單位所在班級我更新我的數據庫或插入對象到數據庫:
private void UpdateObjectToDB()
{
PUnit pUinit = new PUnit()
{
Id = this.Id,
IsStarted = this.IsStarted,
CImage = this.CImage
};
App.database.Update(pUinit);
}
public int InsertObjectToDB()
{
PUnit pUnit = new PUnit()
{
IsStarted = this.IsStarted,
CCImage = this.CImage
};
return App.database.AddItem(pUnit);
}
在哪裏我必須轉換和如何實現?感謝您的回答或建議。
這裏的一個教程,包括如何保存'字節[]'到SQLite數據庫:https://stackoverflow.com/questions/41337487/how -to-下載圖像從-URL並節省,這對A-本地的SQLite數據庫/ 41337488#41337488 –