Home > Tutorials > Insert image to database

Insert image to database

In order to insert an image into SQLite database, you might need to use BLOB type, which stores binary data (it is, byte array).

Creating a database table:

CREATE TABLE tbl_image (
    image_data BLOB

– Before inserting into database, you need to convert your BItmap image into byte array first then apply it using database query.

Please look at my previous post on “SQLite on Android” and “Create and Use custom Content Provider” for references.

For example, when I insert image using Database adapter:

	public long insert(byte[] image) {
		return mDb.insert(TABLE_NAME, null, createContentValues(image));

	private ContentValues createContentValues(byte[] image) {
		ContentValues cv = new ContentValues();
		cv.put(COL_DATA, image);
		return cv;

– When retrieving from database, you certainly have a byte array of image, what you need to do is to convert byte array back to original image. So, you have to make use of BitmapFactory to decode.

This is how I do encoding and decoding between byte array and bitmap:

package pete.android.study;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;

public class Utilities {

	// convert from bitmap to byte array
	public static byte[] getBytes(Bitmap bitmap) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 0, stream);
		return stream.toByteArray();

	// convert from byte array to bitmap
	public static Bitmap getImage(byte[] image) {
		return BitmapFactory.decodeByteArray(image, 0, image.length);

This is the sample project in which will insert an image to database and display to screen after querying it.

Sample Project

Sample Project

Get project here: Browse Sample Project

Well, hope you like it!



Pete Houston

Categories: Tutorials Tags: , , ,
  1. kevin
    October 9, 2015 at 8:22 pm

    i design my android application but when i stored a picture in my db to retrieve it in my android app is refused to come ,can u help me????????

  2. September 30, 2015 at 10:14 pm

    can you pass me the source code? i cant download your code in the link you give

  3. May 4, 2015 at 5:26 pm

    sir how to image upload mysql database please send video link. i try but not insert please help my sir

  4. Pinoy
    September 23, 2012 at 8:46 pm


    This is very helpful to me, I have already created a cookbook recipe that displays images from database based on drawable to database like the —-> byte[] image2 = Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.adobongbalot)); .

    I will very happy if I can capture (built-in camera) the image and save to database (not drawable to database).

    Could you please helpe me on this.



  5. August 2, 2012 at 3:32 pm

    how to i take a picture with my camera and store it in my applcation database

  6. Ka-es-te-es
    December 4, 2011 at 6:40 am

    thank for tutorial.. but how to apply, insert 10 images to database?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: