Xamarin forms, how to save byte[] to SQLite databa

2019-07-26 16:44发布

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.

2条回答
The star\"
2楼-- · 2019-07-26 17:10

Well, I solved issue by changing this class property to string type:

public class Unit
    {
        [PrimaryKey, AutoIncrement]
        public string CImageBase64 { get; set; }
    }

Then in other unit class I changed bindable data type:

public string CImageBindable
        {
            get
            {
                return base.CImageBase64;
            }
            set
            {
                base.CImageBase64 = value;
                OnPropertyChanged(nameof(CImageBindable));
            }
        }

And here I converted to base 64

    if (this.P.CImageBindable == null)
    {
        CImage.Source = "img.png";
    }
    else
    {
        var imageBytes = Convert.FromBase64String(this.P.CImageBindable);

        MemoryStream st = new MemoryStream(imageBytes);
        ImageSource imgSource = ImageSource.FromStream(() => st);
        CImage.Source = imgSource;
    }

And picture selection converted as well:

IGalleryImageService galleryService = Xamarin.Forms.DependencyService.Get<IGalleryImageService>();
            galleryService.ImageSelected += (o, imageSourceEventArgs) =>
            {
                ActiveParking.CImageBindable = Convert.ToBase64String(imageSourceEventArgs.ImageSource);
                MemoryStream st = new MemoryStream(imageSourceEventArgs.ImageSource);
                ImageSource imgSource = ImageSource.FromStream(() => st);
                (ActiveParking.Page as PTemplate).CImage.Source = imgSource;
            };
            galleryService.SelectImage();
查看更多
贪生不怕死
3楼-- · 2019-07-26 17:31

You can try this

     private void UpdateObjectToDB()
     {
        PUnit pUinit = new PUnit()
        {
            Id = this.Id,
            IsStarted = this.IsStarted,
            CImage = Encoding.ASCII.GetBytes(this.CImage) 
        };
        App.database.Update(pUinit);
    }

    public int InsertObjectToDB()
    {
        PUnit pUnit = new PUnit()
        {
            IsStarted = this.IsStarted,
            CCImage = Encoding.ASCII.GetBytes(this.CImage)
        };
        return App.database.AddItem(pUnit);
}
查看更多
登录 后发表回答