Hi friends,
In this tutorial we are sharing the code to insert the image into db and
retrieve the image from db. I hope this article might be helpful to all
learning developer.
SQLite is really a quick and compact android database technology which
incorporates SQL syntax to create queries and also handle data.
Android SDK by itself provides the SQLite support which without doubt
making the setup as well as utilization process within our applications
with no trouble.
Whenever we make use of an Android SQLite Database all of us undoubtedly
require the help of SQLiteOpenHelper to handle our
data.SQLiteOpenHelper is surely an superb destination to place some
initial values right into the android sqlite database when it is built.
For storing the image we are using the blob type. Blob is a Java interface representing the SQL
BLOB
type.
An SQL
BLOB
type stores a large array of binary data (bytes) as the
value in a column of a database.
The
java.sql.Blob
interface provides methods for setting and
retrieving data in the Blob
, for querying Blob
data length,
and for searching for data within the Blob
. More details about Blob Here
But i think in big level storing the image in database is not valuable
because database sqlite have limited space so store the image path is
valuable.
main_activity.xml
1
2
3
4
5
6
7
8
9
10
11
| <relativelayout android:layout_height= "match_parent" android:layout_width= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" > <textview android:id= "@+id/textView1" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:text= "@string/hello_world" > <imageview android:id= "@+id/imageView_image" android:layout_below= "@+id/textView1" android:layout_height= "wrap_content" android:layout_margintop= "64dp" android:layout_torightof= "@+id/textView1" android:layout_width= "wrap_content" > <button android:id= "@+id/button_insert" android:layout_below= "@+id/imageView_image" android:layout_height= "wrap_content" android:layout_margintop= "45dp" android:layout_torightof= "@+id/textView1" android:layout_width= "wrap_content" android:text= "Insert In DB" > </button><button android:id= "@+id/button_retrieve" android:layout_alignleft= "@+id/button_insert" android:layout_below= "@+id/button_insert" android:layout_height= "wrap_content" android:layout_margintop= "56dp" android:layout_width= "wrap_content" android:text= "Retrieve from DB" > </button></imageview></textview></relativelayout> |
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| package com.sunil.insertimageindb; import java.io.ByteArrayOutputStream; import com.sunil.insertimageindb.R; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private ImageView imageview= null ; private Button btninsert= null ; private Button btnretrive= null ; private MyDataBase mdb= null ; private SQLiteDatabase db= null ; private Cursor c= null ; private byte[] img= null ; private static final String DATABASE_NAME = "ImageDb.db" ; public static final int DATABASE_VERSION = 1; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); btninsert=(Button)findViewById(R.id.button_insert); btnretrive= (Button)findViewById(R.id.button_retrieve); imageview= (ImageView)findViewById(R.id.imageView_image); imageview.setImageResource(0); btninsert.setOnClickListener( this ); btnretrive.setOnClickListener( this ); mdb= new MyDataBase(getApplicationContext(), DATABASE_NAME, null , DATABASE_VERSION); Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream bos= new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, bos); img=bos.toByteArray(); db=mdb.getWritableDatabase(); } @Override public void onClick(View arg0) { if (btninsert==arg0) { ContentValues cv= new ContentValues(); cv.put( "image" , img); db.insert( "tableimage" , null , cv); Toast.makeText( this , "inserted successfully" , Toast.LENGTH_SHORT).show(); } else if (btnretrive==arg0) { String[] col={ "image" }; c=db.query( "tableimage" , col, null , null , null , null , null ); if (c!= null ){ c.moveToFirst(); do { img=c.getBlob(c.getColumnIndex( "image" )); } while (c.moveToNext()); } Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length); imageview.setImageBitmap(b1); Toast.makeText( this , "Retrive successfully" , Toast.LENGTH_SHORT).show(); } } } |
MyDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| package com.sunil.insertimageindb; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class MyDataBase extends SQLiteOpenHelper{ public MyDataBase(Context context, String dbname, CursorFactory factory, int dbversion) { super (context, dbname, factory, dbversion); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL( "create table tableimage(image blob);" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } |
Very informative post.I am really impressed by this incredible precious site.
ReplyDelete