Introduction

This article explains how to store a person's data with an image in a SQLite Database.
Using this application you can get an image from a drawable folder and store it in an SQLite Database and also you can retrieve that image from the SQLite database to be shown on the screen.
Step 1
Use the following procedure to create the sample.
Go to "File" and select "Android Application Project".
sto1
Click "Next".
sto2
Click "Next".
sto3
Click "Next".
sto4
Click "Next".
sto5
Step 2
Create an XML file with the following.
In this XML I use two EditTexts, four TextViews, and an ImageView. We enter the value in an EditText and fetch it in a Java class file to store it in a data base.
  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. <TextView android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:id="@+id/textView1"
  13. android:text="Enter Name"
  14. android:textColor="#cccccc"
  15. android:textStyle="bold"/>
  16. <EditText
  17. android:id="@+id/editText1"
  18. android:layout_height="wrap_content"
  19. android:layout_width="fill_parent"
  20. android:layout_below="@+id/textView"/>
  21. <LinearLayout
  22. android:layout_height="wrap_content"
  23. android:layout_below="@+id/editText1"
  24. android:layout_width="fill_parent"
  25. android:id="@+id/linearLayout"
  26. android:orientation="vertical"
  27. android:layout_marginTop="40dp">
  28. <TextView android:layout_height="wrap_content"
  29. android:layout_width="wrap_content"
  30. android:id="@+id/textView2"
  31. android:text="Enter Age"
  32. android:textColor="#cccccc"
  33. android:textStyle="bold"/>
  34. <EditText
  35. android:layout_height="wrap_content"
  36. android:id="@+id/editText2"
  37. android:layout_width="fill_parent"
  38. android:layout_below="@+id/textView"/>
  39. </LinearLayout>
  40. <Button
  41. android:layout_centerInParent="true"
  42. android:text="Send"
  43. android:layout_below="@+id/linearLayout"
  44. android:layout_height="wrap_content"
  45. android:layout_width="wrap_content"
  46. android:id="@+id/button">
  47. </Button>
  48. <TextView
  49. android:id="@+id/textView3"
  50. android:layout_below="@+id/button"
  51. android:layout_width="wrap_content"
  52. android:layout_height="41dp"
  53. android:textSize="15dp"/>
  54. <TextView
  55. android:id="@+id/textView4"
  56. android:layout_width="wrap_content"
  57. android:layout_height="41dp"
  58. android:layout_below="@+id/textView3"
  59. android:textSize="15dp"/>
  60. <LinearLayout
  61. android:layout_width="wrap_content"
  62. android:layout_marginTop="300dp"
  63. android:layout_height="wrap_content">
  64. <ImageView
  65. android:id="@+id/myimage"
  66. android:layout_width="200dp"
  67. android:layout_height="200dp"
  68. />
  69. </LinearLayout>
  70. </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. /** Called when the activity is first created. */
  22. MyDataBase mdb;
  23. SQLiteDatabase db;
  24. Cursor c;
  25. ImageView iv;
  26. byte[] img, img1;
  27. Bitmap b;
  28. String name, age, getname;
  29. TextView textView3, textView4;
  30. EditText editText1, editText2;
  31. @Override
  32. public void onCreate(Bundle savedInstanceState)
  33. {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36. /*Create the database by calling and ing all the parameters to the constructor of MyDataBase class*/
  37. mdb = new MyDataBase(getApplicationContext(), "imagedata", null, 1);
  38. this.deleteDatabase("imagedata");
  39. iv = (ImageView) findViewById(R.id.myimage);
  40. textView3 = (TextView) findViewById(R.id.textView3);
  41. textView4 = (TextView) findViewById(R.id.textView4);
  42. editText1 = (EditText) findViewById(R.id.editText1);
  43. editText2 = (EditText) findViewById(R.id.editText2);
  44. Button button = (Button) findViewById(R.id.button);
  45. button.setOnClickListener(new OnClickListener()
  46. {
  47. @Override
  48. public void onClick(View v)
  49. {
  50. // TODO Auto-generated method stub
  51. name = editText1.getText().toString();
  52. /* In this i have used BitMap class which is used to put the image in a variable. Bitmapfactory class is used to get the image from the drawable folder by calling decodeResource() method. */
  53. age = editText2.getText().toString();
  54. Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  55. /*create the object of ByteArrayoutputStream class. Now break the image into the byte parts by calling toByteArray() of ByteOutputStream class and store it in a array */
  56. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  57. b.compress(Bitmap.CompressFormat.PNG, 100, bos);
  58. byte[] img = bos.toByteArray();
  59. /*to write in a database call the getWritableDatabase method */
  60. db = mdb.getWritableDatabase();
  61. ContentValues cv = new ContentValues();
  62. cv.put("image", img);
  63. cv.put("name", name);
  64. cv.put("age", age);
  65. db.insert("tableimage", null, cv);
  66. String selectQuery = "SELECT * FROM tableimage";
  67. c = db.rawQuery(selectQuery, null);
  68. if (c != null)
  69. {
  70. c.moveToFirst();
  71. do
  72. {
  73. img1 = c.getBlob(2);
  74. String getname = c.getString(0);
  75. String age = c.getString(1);
  76. } while (c.moveToNext());
  77. }
  78. Bitmap b1 = BitmapFactory.decodeByteArray(img1, 0, img1.length);
  79. iv.setImageBitmap(b1);
  80. textView3.setText(name);
  81. textView4.setText(age);
  82. }
  83. });
  84. }
  85. }
  86. class MyDataBase extends SQLiteOpenHelper
  87. {
  88. public MyDataBase(Context context, String name, CursorFactory factory,
  89. int version)
  90. {
  91. super(context, name, factory, version);
  92. // TODO Auto-generated constructor stub
  93. }
  94. public void onCreate(SQLiteDatabase db)
  95. {
  96. // TODO Auto-generated method stub
  97. db.execSQL("create table tableimage (name text,age int,image blob);");
  98. }
  99. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  100. {
  101. // TODO Auto-generated method stub
  102. }
  103. }
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
Enter your Name and Age as in the following:
sto6
When you click on the button the image will be stored and also be shown on the screen with your name and age. I have selected this image randomly to show that you can use your image.
sto7.jpg