today I am dealing with issue how to save byte array to SQLite database. When I am saving base[] to databse, it's looks like that property holding that array, but actually it is not save to database, when I restart application on device. Maybe I have to convert byte[] to base 64 before putting it to database? If yes, how I can do that? Also maybe there are another way save to sqlite database , without converting to base64?
This is my object class:
public class Unit
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public bool IsStarted { get; set; }
public byte[] CImage { get; set; }
}
Here I am selecting image from gallery and set to bindable value:
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();
here is my other Unit class where I am updating my database or inserting objects to database:
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);
}
Where I have to convert and how to implement that? Thank you for answers or suggestions.
Well, I solved issue by changing this class property to string type:
Then in other unit class I changed bindable data type:
And here I converted to base 64
And picture selection converted as well:
You can try this