Introduction

This article explains how to store an image in an SQLite Database in Android.
This application gets the image from the drawable folder and stores it into a database using an SQLite Database.
Step 1
Create a project like this:
SqlkiteStorage
Step 2
Create an XML file with the following.
In this XML file you will use an ImageView in which you set the image after getting it from the drawable folder.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >"
  10. <ImageView
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:id="@+id/imageView"></ImageView>
  14. </RelativeLayout>
Step 3
Create a Java class file with the following.
  1. package com.imagestorage;
  2. import java.io.ByteArrayOutputStream;
  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.ImageView;
  18. import android.widget.TextView;
  19. public class MainActivity extends Activity
  20. {
  21. /* This is my MainActivity class which extends the Activity class. Activity class provides the onCreate() method so when you run your application your compiler first goes to this method to create the activity. setContentView() method is used to set the layout in your activty. */
  22. /** Called when the activity is first created. */
  23. MyDataBase myDataBase;
  24. SQLiteDatabase database;
  25. Cursor c;
  26. ImageView imageView;
  27. byte[] img, img1;
  28. Bitmap b;
  29. @Override
  30. public void onCreate(Bundle savedInstanceState)
  31. {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. ImageView imageView = (ImageView) findViewById(R.id.imageView);
  35. myDataBase = new MyDataBase(getApplicationContext(), "imagedata", null, 1);
  36. // this.deleteDatabase("imagedata");
  37. /*Bitmap is a binary representation in which a bit or a set of bits correspond to some part of an object such as an image. Here we use the Bitmap class which is provided by the Android system to hold the image. Here I have created the object of Bitmap class to hold the image. bitmap Factory class is used to create the object from various resources including files, streams and byteArrays. */
  38. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  39. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  40. bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
  41. byte[] img = bos.toByteArray();
  42. //getWritebleDatabese() method is used to perform write operation into the database
  43. database = myDataBase.getWritableDatabase();
  44. // content values is used to create empty set of values using the default initial size
  45. ContentValues cv = new ContentValues();
  46. cv.put("image", img);
  47. database.insert("tableimage", null, cv);
  48. String selectQuery = "SELECT * FROM tableimage";
  49. c = database.rawQuery(selectQuery, null);
  50. if (c != null)
  51. {
  52. c.moveToFirst();
  53. do
  54. {
  55. img1 = c.getBlob(c.getColumnIndex("image"));
  56. } while (c.moveToNext());
  57. }
  58. Bitmap b1 = BitmapFactory.decodeByteArray(img1, 0, img1.length);
  59. imageView.setImageBitmap(b1);
  60. }
  61. }
  62. // Mydatabase class is used to create the database by extending the SqliteopenHelper class which provides onCrate() and onUpGrade() method.
  63. class MyDataBase extends SQLiteOpenHelper
  64. {
  65. public MyDataBase(Context context, String name, CursorFactory factory, int version)
  66. {
  67. super(context, name, factory, version);
  68. // TODO Auto-generated constructor stub
  69. }
  70. public void onCreate(SQLiteDatabase db)
  71. {
  72. // TODO Auto-generated method stub
  73. db.execSQL("create table tableimage (image blob);");
  74. }
  75. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  76. {
  77. // TODO Auto-generated method stub
  78. }
  79. }
Step 4
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.imagestorage"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.imagestorage.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
You can see your stored image in the SQLite database in such a way
Go to the Files Explorer then select "Data" -> "Data" then go to "YourDataBase" and click on your database name.
Click on "Data".
Go to your database name.
Output