How to retrive BLOB Image from database in android?

Step 1 : add database in your src folder.

 

import java.io.ByteArrayOutputStream;

import com.blobimageretrive.Data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;

public class DBAdapter {

private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = “ideagist”;

// Image table name
private static final String TABLE_IMAGE = “image”;

// Image Table Columns names
private static final String KEY_IMAGE = “image”;
Context mCtx;
DatabaseHelper mDbHelper;
SQLiteDatabase mDb;
public DBAdapter(Context ctx) {
mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
}

public DBAdapter open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}

public void close() {
mDbHelper.close();
}

public void insertEmpDetails(Data employee) {
ContentValues cv = new ContentValues();
cv.put(KEY_IMAGE, getBytes(employee.getImage()));
mDb.insert(TABLE_IMAGE, null, cv);
}

public Data retriveEmpDetails() throws SQLException {
Cursor cur = mDb.query(true, TABLE_IMAGE, new String[] { KEY_IMAGE}, null, null, null, null, null, null);
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(KEY_IMAGE));
cur.close();
return new Data(mCtx,getPhoto(blob));
}
cur.close();
return null;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE = “CREATE TABLE “+TABLE_IMAGE+”(“+KEY_IMAGE+” BLOB NOT NULL”+”)”;
db.execSQL(CREATE_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(“DROP TABLE IF EXISTS ” + TABLE_IMAGE);
onCreate(db);
}
}
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 getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}

step 2 : add activity file in your src folder.

 

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.blobimageretrive.Database.DBAdapter;

public class MainActivity extends Activity {

Button btn_save,btn_retrive;
ImageView iv_save,iv_retrive;
Context ctx;
Data data;
DBAdapter db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ctx = this;
btn_retrive = (Button) findViewById(R.id.btn_retrive);
btn_save = (Button) findViewById(R.id.btn_save);
iv_save = (ImageView) findViewById(R.id.iv_save);
iv_retrive = (ImageView) findViewById(R.id.iv_retrive);

btn_retrive.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
db.open();
db.retriveEmpDetails();
db.close();

iv_retrive.setImageBitmap(data.getImage());
}catch(Exception e){
Toast.makeText(ctx, “First save image”, Toast.LENGTH_SHORT).show();
}
}
});

btn_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db = new DBAdapter(ctx);
Drawable drawable = iv_save.getDrawable();
if(drawable!=null){
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
data = new Data(ctx,bitmapDrawable.getBitmap());
db.open();
db.insertEmpDetails(data);
db.close();
Toast.makeText(ctx, “Save Successfully”, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ctx, “first set drawable image”, Toast.LENGTH_SHORT).show();
}
}
});
}
}

step 3 : add other java file in your src folder.

 

package com.blobimageretrive;

import android.content.Context;
import android.graphics.Bitmap;

public class Data {
Bitmap image;
Context ctx;
public Data(Context ctx,Bitmap image){
this.ctx = ctx;
this.image = image;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
}

step 4 : add layout file.

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
>

<ImageView
android:id=”@+id/iv_save”
android:layout_width=”fill_parent”
android:layout_height=”150dp”
android:src=”@drawable/a” />

<Button
android:id=”@+id/btn_save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Save” />

<Button
android:id=”@+id/btn_retrive”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Retrive” />

<ImageView
android:id=”@+id/iv_retrive”
android:layout_width=”fill_parent”
android:layout_height=”150dp”
/>
</LinearLayout>