How to open Camera and Gallery by Intent

September 13, 2013

public class MainActivity extends Activity {

ArrayList<HashMap<String, String>> menuItems;// = new

Uri selectedImage; // String>>();
public String selectedPath1 = “NONE”;
private static final int SELECT_FILE1 = 1;
private static final int PICK_Camera_IMAGE = 2;
static final int DATE_DIALOG_ID = 0;
public byte[] data;
public static Bitmap bmpScale;
public static String imagePath = null;
File destination;
ImageView imageview;
Uri imageUri;

private static final int REQUEST_IMAGE = 100;
FileInputStream in;

private Bitmap bitmap;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageview = (ImageView) findViewById(R.id.imgggg);
Button camera = (Button) findViewById(R.id.From_camera);
camera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name = dateToString(new Date(), “yyyy-MM-dd-hh-mm-ss”);
destination = new File(Environment
.getExternalStorageDirectory(), name + “.jpg”);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, PICK_Camera_IMAGE);

}
});

Button gallery = (Button) findViewById(R.id.From_gallery);
gallery.setOnClickListener(new OnClickListener() {

//
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(SELECT_FILE1);

}
});

}

// ………………………………..

public void openGallery(int SELECT_FILE1) {

Intent intent = new Intent();
intent.setType(“image/*”);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, “Select file to upload “),
SELECT_FILE1);

}

 // …………..intent…………………………………..
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch (requestCode) {
case SELECT_FILE1:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();

if (requestCode == SELECT_FILE1) {
selectedPath1 = getPath(selectedImage);
// mimagepath.setText(selectedPath1);

if (selectedPath1 != null) {

BitmapFactory.Options options = new BitmapFactory.Options();

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(selectedPath1, options);

int bitmapWidth = 400;
int bitmapHeight = 250;
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > bitmapHeight || width > bitmapWidth) {
if (width > height) {
inSampleSize = Math.round((float) height
/ (float) bitmapHeight);
} else {
inSampleSize = Math.round((float) width
/ (float) bitmapWidth);
}
}

options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;

Bitmap bmpScale = BitmapFactory.decodeFile(
selectedPath1, options);
imageview.setImageBitmap(bmpScale);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

bmpScale.compress(CompressFormat.JPEG, 100, bos);
bmpScale.compress(CompressFormat.JPEG, 100, bos);
// **********************
// globaly…………………………….
data = bos.toByteArray();

}

}
}

break;
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {

try {
in = new FileInputStream(destination);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
imagePath = destination.getAbsolutePath();

bmpScale = BitmapFactory.decodeStream(in, null, options);
imageview.setImageBitmap(bmpScale);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

bmpScale.compress(CompressFormat.JPEG, 50, bos);

data = bos.toByteArray();

// bmpScale= null;

} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, “Picture was not taken”,
Toast.LENGTH_SHORT).show();
}

}

}

// getting a path……………………….function…………….
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

// ……………..date function………………………………….
public String dateToString(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}

 // ……decode the image…………………………………
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 1024;

// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}

     // Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);

// imgView.setImageBitmap(bitmap);

}
}

///…………………..Xml……………………..

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity” >

<Button
android:id=”@+id/From_gallery”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/From_camera”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”73dp”
android:text=”From_camera” />

<Button
android:id=”@+id/From_camera”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/From_gallery”
android:layout_alignParentTop=”true”
android:layout_marginTop=”196dp”
android:text=”From_gallery”
android:visibility=”visible” />

<ImageView
android:id=”@+id/imgggg”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”57dp”
android:src=”@drawable/ic_launcher” />

</RelativeLayout>

Capture

Manifest Permission
<uses-permission android:name=”android.permission.CAMERA” />

<uses-feature android:name=”android.hardware.camera” />
<uses-feature android:name=”android.hardware.camera.autofocus” />

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

DataBase In Android

August 21, 2013

How to create Data base
step 1. Create Project database.AndroidRishi
Step 2. Create  MainClass.
package ex.com;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainClass extends Activity {

private DadaBaseClass _smsLogDataBase;

private EditText Password, Name, ID;

private Button Save1, update, delete, retrive;

public ArrayList newlogg = DadaBaseClass.logg;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);

Name = (EditText) findViewById(R.id.editname);

ID = (EditText) findViewById(R.id.editID);

Password = (EditText) findViewById(R.id.editpassword);

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

update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
_smsLogDataBase = new DadaBaseClass(
MainClass.this);
_smsLogDataBase.open();
// _smsLogDataBase.insertTitle(ID.getText().toString(),Name.getText().toString(),Password.getText().toString());

_smsLogDataBase.updateTitle2(ID.getText().toString(), Name
.getText().toString());

}
});

delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
_smsLogDataBase = new DadaBaseClass(
MainClass.this);
_smsLogDataBase.open();
// _smsLogDataBase.insertTitle(ID.getText().toString(),
// Name.getText().toString(), Password.getText().toString());

_smsLogDataBase.deleteTitle(Name.getText().toString());

}
});

retrive = (Button) findViewById(R.id.retrive);
retrive.setOnClickListener(new OnClickListener() {

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

_smsLogDataBase = new DadaBaseClass(
MainClass.this);
_smsLogDataBase.open();
_smsLogDataBase.getAll1();

Intent i = new Intent(MainClass.this, List.class);

startActivity(i);

}
});

Save1 = (Button) findViewById(R.id.save1);
Save1.setOnClickListener(new OnClickListener() {

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

_smsLogDataBase = new DadaBaseClass(
MainClass.this);
_smsLogDataBase.open();
_smsLogDataBase.insertTitle(ID.getText().toString(), Name
.getText().toString(), Password.getText().toString());

ID.setText(“”);
Name.setText(“”);
Password.setText(“”);

// _smsLogDataBase.getAll1();
}
});

}
}

//………………… create Xml file for MainClass ,main1.xml

Step 3. Create DadaBaseClass

package ex.com;

import java.util.ArrayList;

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.util.Log;

public class DadaBaseClass {

// ……To set the row id and fields which we are using in data
// base…………..//

public static final String KEY_ROWID = “_id”;
public static final String KEY_id = “id”;
public static final String KEY_name = “username”;
public static final String KEY_password = “passwordtxt”;

// …….Set the data base name and table
// …………………………………..//

private static final String TAG = “DBAdapter”;
private static final String DATABASE_NAME = “sqltitedemodatabase”;// database
// name
private static final String DATABASE_TABLE = “sqlitedemo”;// table name

private static final int DATABASE_VERSION = 1;// version
private static String COLS[] = { “_id”, “id”, “username”, “passwordtxt” };// coloumns
private static final String DATABASE_CREATE = “create table sqlitedemo(_id integer primary key autoincrement, “
+ “id text not null UNIQUE, username text not null,passwordtxt text not null);”;

// ……Query to creat the table in data base …………………..//

public static ArrayList logg;

public static String name;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DadaBaseClass(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
Log.d(“11111111″, “hihiihihiihihihihihihihi”);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, “Upgrading database from version ” + oldVersion + ” to “
+ newVersion + “, which will destroy all old data”);
db.execSQL(“DROP TABLE IF EXISTS titles”);
onCreate(db);
}
}

// —opens the database—
public DadaBaseClass open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}

// —closes the database—
public void close() {
DBHelper.close();
}

// —insert a title into the database—
public long insertTitle(String id, String name, String password) {

Log.d(“messageeeeeeeeeeeeeee    “, “Databse Message” + password);
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_id, id);
initialValues.put(KEY_name, name);
initialValues.put(KEY_password, password);

return db.insert(DATABASE_TABLE, null, initialValues);
}

// —deletes a particular title—
public boolean deleteTitle(String rowId) {
Log.d(“deleteeeeeee”, “deleteeeeeeeeee”);
return db.delete(DATABASE_TABLE, “username” + “=’” + rowId + “‘”, null) > 0;
}

// —retrieves all the titles—
public Cursor getAllTitles() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_name },
null, null, null, null, null);
}

public void getAll1() {

logg = new ArrayList();
Cursor c = null;
try {
// c = this.db.query(SmsLogDataBase.DATABASE_TABLE,
// SmsLogDataBase.COLS, null, null, null, null, null);

c = this.db.query(DadaBaseClass.DATABASE_TABLE,new String[] { KEY_name }, null, null, null, null, null);
int numRows = c.getCount();
c.moveToFirst();
for (int i = 0; i  0;
}
}

Step 4
Create List class

package ex.com;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class List extends Activity {

//public static  ArrayList log;

ListView mlist;

public ArrayList newlogg=DadaBaseClass.logg;

private String[] Arr;

private ArrayAdapter adapter1;

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

mlist=(ListView)findViewById(R.id.list);

if(newlogg.size()>0){

//_adaptor= new LazyAdapter(MapMe.this,mLocationInfo);
//mList.setAdapter(_adaptor);
Arr=new String[newlogg.size()];
for(int i=0;i<newlogg.size();i++){

Arr[i]=newlogg.get(i);
}

adapter1=new ArrayAdapter(List.this,android.R.layout.simple_list_item_1, Arr);

mlist.setAdapter((adapter1));

}
}
}
//// create xml file

Follow

Get every new post delivered to your Inbox.