How to add uma image to a database in SQLite with Button?
-
I'm confused about what you are asking. Could you please details your issue? What "how introduced image" mean?Sunteen Wu– Sunteen Wu2017-07-24 01:53:22 +00:00Commented Jul 24, 2017 at 1:53
-
I want the Image to come out in the SqLiteuser3682557– user36825572017-07-24 12:09:02 +00:00Commented Jul 24, 2017 at 12:09
2 Answers
If you just want to insert images to SQLite, you can convert the image to byte array and save it in a blob column of a SQLite table. SQLite3 can store a blob datatype. Details about how to do you may reference this blog. And you could also encode the image byte array to Base64 string and save it to table as string column. Details for how to encode and decode Base64 please reference this document.
And I recommend you to only save the image paths to the database, not the image itself.
More details please reference this similar thread.
Comments
I do not recommend saving a byte[] in the database. Saving the image directly in the database only makes sense if the database is accessible by other users with other users who do not have a local file of the image. Converting the image data into a byte[], or Base64, is slower than using the file system.
Instead, as Sunteen mentions, save the image to LocalFolder and then you can save a simple string of the file path in the database.
You can easily get the file path from the StorageFile object.
As an example, I've updated your code so that it saves the StorageFile returned from CaptureFileAsync in the app's LocalFolder. Then you can just use that path to set the Image in the UI and save that string to the database item being added.
Using your code, here's the approach:
// a private field on the class level
private string _currentImageFilePath;
private async void btcapture_Click(object sender, RoutedEventArgs e)
{
var cameraUI = new CameraCaptureUI();
cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
cameraUI.PhotoSettings.CroppedSizeInPixels = new Size(150, 150);
var photo = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null) return;
// Create a unique filename
var uniqueFileName = $"{Guid.NewGuid()}.jpg";
// Save the photo file in the app's Local folder
var savedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(uniqueFileName, CreationCollisionOption.GenerateUniqueName);
// -- Now you have an easy to store string using the file's Path property -- //
// 1 - Use this string to save to the database
_currentImageFilePath = savedFile.Path;
// 2 - You can also use the same file path to set the <Image/> source
// you do NOT need to decode the image manually
Capture.Source = new BitmapImage(new Uri(savedFile.Path));
}
private async Task SaveToDatabase()
{
var selectedDate = Data1.Date.UtcDateTime;
var Db_Pet = new DataBaseHelper();
if (txtnombre.Text != "" & txtchip.Text != "" & txtraza.Text != "" & Selectedgenero.Text != "" & txtcolor.Text != "")
{
var petToInsert = new Mascota
{
Name = txtnombre.Text,
Chip = txtchip.Text,
ImageFilePath = _currentImageFilePath
};
Db_Pet.Insert(petToInsert);
}
else
{
await new MessageDialog("Enter your pet").ShowAsync();
}
}
Note that there are some scenarios where saving the data directly in the database makes sense: if you're using that database where the app doesnt have the image files saved locally (think SQL Server with many users, or restore scenarios)