Wednesday 12 December 2012

Image save to database and retrive


Database.class:
package com.venkool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class DataBase extends Activity {
/** Called when the activity is first created. */
private static final int CAMERA_REQUEST = 1888;
ImageView ivImage, ivImage1;
Button btnCamera;

MyDatabase mydatabase;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);

mydatabase = new MyDatabase(DataBase.this);

ivImage = (ImageView) findViewById(R.id.ivImage);

ivImage1 = (ImageView) findViewById(R.id.ivImage1);

// For taking drawable resources
// Bitmap bitmap = (Bitmap) ((BitmapDrawable)
// getResources().getDrawable(
// R.drawable.my)).getBitmap();


btnCamera = (Button) findViewById(R.id.btnCamera);

btnCamera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo2 = (Bitmap) data.getExtras().get("data");
ivImage.setImageBitmap(photo2);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) ivImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] photo = baos.toByteArray();
mydatabase.insert(photo);

byte[] photo1 = mydatabase.getBlog();
ByteArrayInputStream imageStream = new ByteArrayInputStream(photo1);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
ivImage1.setImageBitmap(theImage);
}
}
}


2.Adapter Helper

package com.venkool;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabase extends Activity {
/** Called when the activity is first created. */

DataBaseHelper dataBaseHelper;
SQLiteDatabase sqLiteDatabase;

public MyDatabase(Context context) {
dataBaseHelper = new DataBaseHelper(context, "MyDB.sqlite", null, 1);
}

class DataBaseHelper extends SQLiteOpenHelper {

public DataBaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table tblImage(image BLOB)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

public void insert(byte[] photo) {
open();

ContentValues values = new ContentValues();
try {

values.put("image", photo);
sqLiteDatabase.insert("tblImage", null, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}

public void open() {
sqLiteDatabase = dataBaseHelper.getWritableDatabase();
}

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

public Cursor get() {
return sqLiteDatabase.query("tblImage", null, null, null, null, null,
null);
}

public byte[] getBlog() {

byte[] photo = null;

Cursor c = null;
open();
c = get();

while (c.moveToNext()) {

photo = c.getBlob(0);

return photo;
}
close();
return photo;
}

}

3.database.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:src="@drawable/my"
            android:id="@+id/ivImage"
            android:layout_width="150dp"
            android:layout_height="150dp" />

        <ImageView
            android:id="@+id/ivImage1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnCamera"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Camera" />

    <EditText
        android:id="@+id/edtImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

4.