Skip to content Skip to sidebar Skip to footer

How To Store And Retrieve Images In Sqlite Database In Android?

I am working on quiz application and I need to display questions along with images. I have stored the questions in sqlite database and displaying them in android. But I dont know h

Solution 1:

You generally don't save binary data to a database. It's considered bad form and can make queries to the database take really long because of the way the data ends up getting stored on the disk. What you should do is store a file path to the image in your database and store the image on the sdcard.

Without writing massive amounts of code for you, here's a high-level example. First, create your table to store the image with the following sql call:

CREATE TABLE images (_id INTEGER PRIMARY KEY AUTOICREMENT, file_path TEXT, name TEXT);

When it comes time to insert an image you have to do two things. First, write the image out to the SD card. Follow the instructions here to do that. Once you've written it out and gotten a file name for it, you just do a sql query like so:

INSERT INTO images (file_path, name) VALUES ("your file path", "name of image");

Whenever you actually need to display the image, you just read the file specified by file_path from your sdcard.


Solution 2:

In the spirit of actually answering your stated question, if you REALLY want to write the image to the DB you would add a BLOB field and store the data in that, but there's no reason to do this... just follow Kurtis Nusbaum's advice and store the image as a file with a reference in the db row as to where it is.


Post a Comment for "How To Store And Retrieve Images In Sqlite Database In Android?"