locked
How to save and retrieve Image from Sqlite ? RRS feed

  • Question

  • User369473 posted

    I used this code to get Image from Gallery

    but now I don't know how to save it to sqlite database and retrieve it ??? Any solution ?

    Monday, August 13, 2018 12:26 PM

All replies

  • User171749 posted

    You can either store the file path to the image as a string, or write the image as a byte[].

    If I create a class like this ```CSHARP public class MediaItemToUpload { [PrimaryKey, AutoIncrement] public int primaryKey { get; set; }

        public byte[] itemData { get; set; }
    
        public MediaType mediaType { get; set; }
    }
    

    ``` and create the SQLite table based on the MediaToUpload class, then when I can insert images into the database. You need to familiarise yourself with the SQLite nuget but some quick pointers are...

    Create Table: CSHARP db.CreateTable<MediaItemToUpload>();

    Insert into Table: CSHARP db.Insert(new MediaItemToUpload { mediaType = MediaType.Audio, itemData = new byte[] ) });

    Retrieve from Table: CSHARP MediaItemToUpload mediaItem = db.Table<MediaItemToUpload>().FirstOrDefault();

    Monday, August 13, 2018 12:31 PM
  • User369473 posted

    @seanyda said: You can either store the file path to the image as a string, or write the image as a byte[].

    If I create a class like this ```CSHARP public class MediaItemToUpload { [PrimaryKey, AutoIncrement] public int primaryKey { get; set; }

      public byte[] itemData { get; set; }
    
      public MediaType mediaType { get; set; }
    

    } ``` and create the SQLite table based on the MediaToUpload class, then when I can insert images into the database. You need to familiarise yourself with the SQLite nuget but some quick pointers are...

    Create Table: CSHARP db.CreateTable<MediaItemToUpload>();

    Insert into Table: CSHARP db.Insert(new MediaItemToUpload { mediaType = MediaType.Audio, itemData = new byte[] ) });

    Retrieve from Table: CSHARP MediaItemToUpload mediaItem = db.Table<MediaItemToUpload>().FirstOrDefault();

    Can I this code to save the Image that I got from gallery in database

    Monday, August 13, 2018 12:42 PM
  • User171749 posted

    @Endrit14 said:

    @seanyda said: You can either store the file path to the image as a string, or write the image as a byte[].

    If I create a class like this ```CSHARP public class MediaItemToUpload { [PrimaryKey, AutoIncrement] public int primaryKey { get; set; }

        public byte[] itemData { get; set; }
    
        public MediaType mediaType { get; set; }
    }
    

    ``` and create the SQLite table based on the MediaToUpload class, then when I can insert images into the database. You need to familiarise yourself with the SQLite nuget but some quick pointers are...

    Create Table: CSHARP db.CreateTable<MediaItemToUpload>();

    Insert into Table: CSHARP db.Insert(new MediaItemToUpload { mediaType = MediaType.Audio, itemData = new byte[] ) });

    Retrieve from Table: CSHARP MediaItemToUpload mediaItem = db.Table<MediaItemToUpload>().FirstOrDefault();

    Can I this code to save the Image that I got from gallery in database

    No, You're not even updating the database doing it like this. You need to retrieve the user record from the database, then modify it, then call Update();

    Chances are, You probably don't even have any records for User in the database at the moment. I can see you're setting the Image Source property using a MemoryStream byte[]. This is stored in memory, You can't covert that "Source" into a string and pull it back to be used later, there is no image data to save/retrieve.

    Monday, August 13, 2018 12:45 PM