Search This Blog

Saturday, 12 May 2012

Bitmap operations like re sizing, rotating bitmap and other operations

In programming, Image processing is the most difficult work. All though i am not going to discuss image processing in depth but we will discuss about bitmap basic operation like re sizing, rotating bitmap, how to create bitmap from file , input stream and resource.we will discuss it step by step and finally you will get source in which you can enjoy playing with it. img is the ImageView object in my project.
As we are going to discuss bitmap to we need to study how to avoid Memory Over Flow while using big image

Friday, 11 May 2012

Android Database SQLite tutorial : Insert, update, delete, deleting data base and table

All though we know mobile do not have large memory to save data in comparison of Desktop and Laptop
but data base is highly important in android (and other mobile OS also).
In android we use SQlite database. Sq-lite is light wight and design according to support mobile device limited memory.
First i am listing some basic operation and will explain every thing with example
1) Creating data base - In android, we have an API classes to create it that is  SQLiteOpenHelper to create data base. Best way is to create a different class and extends SQLiteOpenHelper

package com.gu;

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

public class DataBaseHub extends SQLiteOpenHelper {

    private static final String dbname = "demo.db";
    private static final int version = 2;
    public static String Ename="Ename";
    public static String Eid="Eid";
    public static String Eadd="Eadd";
    public static String Emp="Emp";
    
    public DataBaseHub(Context context) {
        super(context, dbname, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String employee1 = "create table "+Emp+"("+Eid+" integer primary key,"+Ename+" tex                            t,"+Eadd+" text)";
        db.execSQL(employee1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (oldVersion < newVersion) {
            String employee1 = "create table emp("+Eid+" integer,"+Ename+" text,"+Eadd+" t            ext)";
            db.execSQL(employee1);
        }
    }
}

2)Creating a data base Table

String employee1 = "create table "+Emp+"("+Eid+" integer primary key,"+Ename+" text,"+Eadd+" text)";
db.execSQL(employee1);

2)Open Data Base - when we open data base in two way either we want to open data base only for reading or for writing into data base. If we open data base in writing mode then we will get access to reading automatically.

  DataBaseHub dbh=new DataBaseHub(activitycontext);                                       
SqliteDatabase db= dbhgetWritableDatabase();                                                    SqliteDatabase db= dbh.getReadableDatabase();

3) Inserting Values into data base - Now we have created data base and open it for performing operation on it so basic operation is to insert value into data base. I have taken table so i will insert values into this table

DataBaseHub dbh=new DataBaseHub(this);
SQLiteDatabase db=dbh.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(DataBaseHub.Eid,101);
cv.put(DataBaseHub.Ename,"Tofeeq");
cv.put(DataBaseHub.Eadd,"142,Ananad Delhi");
long i=db.insert(DataBaseHub.Emp, null, cv);
Log.i("Row ID=",String.valueOf(i));

4) Deleting particular row from data base -  Deleting  particular row in data base is damn simple. We have to specify column name( to identify which row we want to delete)
DataBaseHub dbh=new DataBaseHub(this);
SQLiteDatabase db=dbh.getWritableDatabase();
i=db.delete(DataBaseHub.Emp, DataBaseHub.Eid+"=?",new String[]{"101"});
Log.i("Number of Row=",String.valueOf(i)););

5)Updating a row into data base - Updating row into data base is little bit complicated so i will explain there in code

DataBaseHub dbh=new DataBaseHub(this);
SQLiteDatabase db=dbh.getWritableDatabase();
// Create content values that contains the name of the column you want to update and the value you want to assign to it 
ContentValues cv = new ContentValues();
cv.put("my_column", "5");
String where = DataBaseHub.Eid+"=?"; // The where clause to identify which columns to update.
String[] value = { "2" }; // The value for the where clause.
// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5)
db.update(DataBaseHub.Emp, cv, where, value);

6) Reading record from data base - you can read all record by querying a table it will store table records into Cursor. Use cursor method's cusor.movetoNext() cursor.movetToprevious()

DataBaseHub dbh=new DataBaseHub(this);
SQLiteDatabase db = dbh.getReadableDatabase();
Cursor cursor = db.query("Table_name",null,null,null,null,
           null, null, null, null, null);
    if (cursor != null)
    cursor.moveToFirst();
//Now you can read record from cursor easily

7) Deleting Table from data base - Deleting table in android data base is very important. E.g if you are making Music Player. Then you need to create dynamic table while you creating play list

DataBaseHub dbh=new DataBaseHub(this);
SQLiteDatabase db=dbh.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

8) Deleting complete data base - all though we do not need this operation generally but in case if you need to delete your data base you can delete easily by using activity context.


context.deleteDatabase(DATABASE_NAME);

Sunday, 6 May 2012

Taking picture and Video from Camera and displaying or playing in android

Taking picture in android has two way to achieve it

1) Using  Surface view with Camera integration
2)Starting Default Camera Activity that will return an intent with picture

first way i have discussed in How to take picture with surface View and camera .Now i am going to discuss second way of taking picture from android camera. And In code you will also know how to take Video in android and Playing it. I will display taken image in ImageView and video will be start paying in a videoview. User Interface will be similar to this when you will run code


Source code and screen shot are available at the end of article

private void dispatchTakePictureIntent(int actionCode) {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = null;
            try {
                f = setUpPhotoFile();
                mCurrentPhotoPath = f.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            } catch (IOException e) {
                e.printStackTrace();
                f = null;
                mCurrentPhotoPath = null;
       } // switch
startActivityForResult(takePictureIntent, actionCode);
    }

This method will dispatch a intent and will start an Camera Activity . In my code i have two option weather you want to take big picture or small one (to avoid memory error ). So you will get a switch case there. actioncode is decide, we will have to handle big Image or small Image.

Here are the step to create file path to save image

setUpPhotoFile() this method is used to create a file for saving image

 mCurrentPhotoPath = f.getAbsolutePath(); // this will save the file path and will pass into intent so that it will take it to save image later on

finally we will start activity

startActivityForResult(takePictureIntent, actionCode);

Now if we click picture then we have to handle intent data inside the onActivityResult()

private void handleSmallCameraPhoto(Intent intent) {
  Bundle extras = intent.getExtras();
  mImageBitmap = (Bitmap) extras.get("data");
  mImageView.setImageBitmap(mImageBitmap);
  mVideoUri = null;
  mImageView.setVisibility(View.VISIBLE);
  mVideoView.setVisibility(View.INVISIBLE);
 }

In activity result i have three method  one for handle small picture intent, second to handle big picture then one for handle Video .If you are taking video or big picture then study these method


 private void handleBigCameraPhoto() {

  if (mCurrentPhotoPath != null) {
   setPic();
   galleryAddPic();
   mCurrentPhotoPath = null;
  }
 }



      private void handleCameraVideo(Intent intent) {
  mVideoUri = intent.getData();
  mVideoView.setVideoURI(mVideoUri);
  mImageBitmap = null;
  mVideoView.setVisibility(View.VISIBLE);
  mImageView.setVisibility(View.INVISIBLE);
 }

                                                        Screen shot of this project



    

Source Code

Android News and source code