Thursday, 15 August 2013

Image Save and Retrive from DB Demo

Demo:

1)MainActivity.java: 
package com.venky.imageanddb;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
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 {

      ImageView iv1, iv2, iv3, iv4, iv5, imgDB, selectedImg = null;
      Database db;
      boolean status = false;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            db = new Database(this);

            iv1 = (ImageView) findViewById(R.id.imageView1);
            iv2 = (ImageView) findViewById(R.id.imageView2);
            iv3 = (ImageView) findViewById(R.id.imageView3);
            iv4 = (ImageView) findViewById(R.id.imageView4);
            iv5 = (ImageView) findViewById(R.id.imageView5);
            selectedImg = (ImageView) findViewById(R.id.selectedImg);

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

            iv1.setOnClickListener(this);
            iv2.setOnClickListener(this);
            iv3.setOnClickListener(this);
            iv4.setOnClickListener(this);
            iv5.setOnClickListener(this);

            Button btnSave = (Button) findViewById(R.id.btnSave);
            Button btnRrtrive = (Button) findViewById(R.id.btnRetrive);

            btnSave.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        if (status) {
                              ByteArrayOutputStream baos = new ByteArrayOutputStream();
                              BitmapDrawable drawable = (BitmapDrawable) selectedImg
                                          .getDrawable();
                              Bitmap bitmap = drawable.getBitmap();
                              bitmap.compress(CompressFormat.PNG, 100, baos);
                              byte[] img = baos.toByteArray();

                              boolean flag = db.insert(img);

                              if (flag)
                                    Toast.makeText(MainActivity.this,
                                                "Successfully inserted image into DB", 3000)
                                                .show();
                              else
                                    Toast.makeText(MainActivity.this,
                                                "failed to Image insert into DB", 3000).show();
                        } else
                              Toast.makeText(MainActivity.this,
                                          "U are not selected any image", 3000).show();
                  }
            });

            btnRrtrive.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        Database db = new Database(MainActivity.this);

                        byte[] img = null;
                        Cursor c = db.getDBValues();
                        int count = c.getCount() - 1;
                        if (count != -1) {
                              c.moveToPosition(count);
                              img = c.getBlob(0);

                              ByteArrayInputStream bais = new ByteArrayInputStream(img);
                              Bitmap bitmap = BitmapFactory.decodeStream(bais);
                              imgDB.setImageBitmap(bitmap);
                        } else
                              Toast.makeText(MainActivity.this, "No Image in DB", 3000)
                                          .show();
                  }
            });
      }

      @Override
      public void onClick(View v) {
            BitmapDrawable drawable;
            Bitmap bitmap;
            switch (v.getId()) {
            case R.id.imageView1:
                  drawable = (BitmapDrawable) iv1.getDrawable();
                  bitmap = drawable.getBitmap();
                  selectedImg.setImageBitmap(bitmap);
                  status = true;
                  break;

            case R.id.imageView2:
                  drawable = (BitmapDrawable) iv2.getDrawable();
                  bitmap = drawable.getBitmap();
                  selectedImg.setImageBitmap(bitmap);
                  status = true;
                  break;
            case R.id.imageView3:
                  drawable = (BitmapDrawable) iv3.getDrawable();
                  bitmap = drawable.getBitmap();
                  selectedImg.setImageBitmap(bitmap);
                  status = true;
                  break;
            case R.id.imageView4:
                  drawable = (BitmapDrawable) iv4.getDrawable();
                  bitmap = drawable.getBitmap();
                  selectedImg.setImageBitmap(bitmap);
                  status = true;
                  break;
            case R.id.imageView5:
                  drawable = (BitmapDrawable) iv5.getDrawable();
                  bitmap = drawable.getBitmap();
                  selectedImg.setImageBitmap(bitmap);
                  status = true;
                  break;
            }
      }
}


2)Database.java: 
package com.venky.imageanddb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {

      SQLiteDatabase db;

      public Database(Context context) {
            super(context, "MyDB", null, 1);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table img_tbl(img blob)");
      }

      @Override
      public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

      }

      boolean insert(byte[] img) {

            try {
                  db = getWritableDatabase();
                  ContentValues values = new ContentValues();
                  values.put("img", img);
                  db.insert("img_tbl", null, values);
                  return true;
            } catch (Exception e) {
            }

            return false;

      }

      Cursor getDBValues() {

            Cursor c = null;
            try {
                  db = getWritableDatabase();
                  c = db.query("img_tbl", null, null, null, null, null, null);
            } catch (Exception e) {
            }
            return c;

      }

}

3)main.xml: 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="Select Image"
        android:textStyle="bold" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:src="@drawable/orange" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:src="@drawable/mango" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:src="@drawable/graps" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:src="@drawable/apple" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:src="@drawable/banana" />
        </LinearLayout>
    </HorizontalScrollView>

    <ImageView
        android:id="@+id/selectedImg"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="U want save this image"
        android:textStyle="bold"
        android:visibility="invisible" />

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

        <Button
            android:id="@+id/btnSave"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Save" />

        <Button
            android:id="@+id/btnRetrive"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Retrive" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imgDB"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center" />

</LinearLayout>

4)Download this Project Click Here  

No comments:

Post a Comment