Monday, June 6, 2016

Capture Image from Camera and Store/Retrieve to/from SQLite Database

 Jai Saraswati Maa"
Hi .... Dear All ....  Today I am uploading a superb example of Image with SQLite Database
In this example you can capture image from Camera and store in database as well as you can retrieve image from database,
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
Special Thanks Dilip Singh 
@@@@ please add read write permission in manifest file like
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

A. Output will be like this 

B. XML file activity_main.xml is as

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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: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="com.example.student.imagesqlite.MainActivity">


    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Click Image"

        android:id="@+id/btnSelect"

        android:onClick="clickImage"

        android:layout_alignTop="@+id/btnUpload"

        android:layout_alignParentStart="true"/>

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="150dp"

        android:id="@+id/imageView"

        android:src="@mipmap/ic_launcher"

        android:layout_below="@+id/btnUpload"

        android:layout_centerHorizontal="true" />
    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Image Upload "

        android:id="@+id/btnUpload"

        android:onClick="sand"

        android:layout_marginTop="0dp"

        android:layout_alignParentTop="true"

        android:layout_alignParentEnd="true" />
    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Show Image"

        android:id="@+id/button"

        android:onClick="show"

        android:layout_below="@+id/imageView"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="67dp" />
    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageView1"

        android:src="@mipmap/ic_launcher"

        android:layout_alignParentBottom="true"

        android:layout_alignStart="@+id/imageView"

        android:layout_marginTop="300dp" />

</RelativeLayout>

C. Java Files are 

1. MainActivity.java is as 

package com.example.student.imagesqlite;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {
    ImageView imgv1,imgv2;
    Bitmap bm = null;
    int REQUEST_CHECK = 0;
    byte img[];
    private MyDataBase mdb=null;
    private SQLiteDatabase db=null;
    private Cursor c=null;
    private static final String DB_NAME = "ImgDb.db";
    public static final int DB_VERSION = 2;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgv1 = (ImageView) findViewById(R.id.imageView);
        imgv2 = (ImageView) findViewById(R.id.imageView1);

        mdb=new MyDataBase(getApplicationContext(), DB_NAME,null,

                DB_VERSION);

    }
    public void clickImage(View view)
    {

        //fire intent

        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, REQUEST_CHECK);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CHECK)
            onCaptureImageResult(data);


    }

    private void onCaptureImageResult(Intent data) {

        bm = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
        img = bos.toByteArray();
        imgv1.setImageBitmap(bm);

    }



    public void sand(View view)
    {
        db=mdb.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("image", img);
        db.insert("tableimage", null, cv);

        Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();

    }
    public void show(View view)
    {
        String[] col={"image"};
        db=mdb.getReadableDatabase();
        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);

        imgv2.setImageBitmap(b1);
        Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
    }
}
2. MyDataBase.java is as 

package com.example.student.imagesqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDataBase extends SQLiteOpenHelper {

    public MyDataBase(Context context, String dbname,

                      SQLiteDatabase.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) {

    }
}
 


No comments:

Post a Comment