Xamarin forms, how to save byte[] to SQLite database?

丶灬走出姿态 提交于 2019-12-11 06:20:43

问题


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.


回答1:


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();



回答2:


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);
}


来源:https://stackoverflow.com/questions/38560168/xamarin-forms-how-to-save-byte-to-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!