File f = File(context.getCacheDir(), filename);
f.createNewFile();
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
Android and Java Development
Sunday, 9 June 2013
Bitmap to File in Android
Friday, 19 April 2013
How Run Other App using intent in android
Intent i = new Intent(); PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("app package name"); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i)
Wednesday, 13 March 2013
UnZip file in Android
With the help of this function we can unzip file with in any given folder in sdcard :
public static boolean UnZipFolder(String zipFileString, String outPathString)throws Exception {
android.util.Log.v("XZip", "UnZipFolder(String, String)");
java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
java.util.zip.ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
szName = szName.substring(0, szName.length() - 1);
java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
folder.mkdirs();
} else {
try{
java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
file.createNewFile();
java.io.FileOutputStream out = new java.io.FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
while ((len = inZip.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
out.close();
}catch(Exception e){
Log.d("UI","unzip error!");
}
}
}
inZip.close();
return true;
public static boolean UnZipFolder(String zipFileString, String outPathString)throws Exception {
android.util.Log.v("XZip", "UnZipFolder(String, String)");
java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
java.util.zip.ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
szName = szName.substring(0, szName.length() - 1);
java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
folder.mkdirs();
} else {
try{
java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
file.createNewFile();
java.io.FileOutputStream out = new java.io.FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
while ((len = inZip.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
out.close();
}catch(Exception e){
Log.d("UI","unzip error!");
}
}
}
inZip.close();
return true;
}
Tuesday, 5 February 2013
How to use external database in android
Here in code for use external database in android project.
1) first put your .db file(database file ) in assets folder
2) then use below code for copy database into project package.
package com.mobidhan.databasecopy;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DataBaseCopy extends SQLiteOpenHelper{
private static String database_n;//database name
private static String database_p ;//database path
private static final int DATABASE_VERSION = 2;
private static String DatabaseName;
private SQLiteDatabase dataBase;
private final Context dbContext;
//DB_NAME is name of database which is in assets folder
//path is our package name
public DataBaseCopy(Context context,String DB_NAME,String PATH) {
super(context, DB_NAME, null, DATABASE_VERSION);
DatabaseName=DB_NAME;
database_p="/data/data/"+PATH+"/databases/";
this.dbContext = context;
database_n = DatabaseName;
// checking database and open it if exists
if (checkDataBase()) {
openDataBase();
} else
{
try {
this.getReadableDatabase();
copyDataBase();
this.close();
openDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
Toast.makeText(context, "database is created", Toast.LENGTH_LONG).show();
}
}
private void copyDataBase() throws IOException{
InputStream myInput = dbContext.getAssets().open(database_n);
String outFileName = database_p + database_n;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String dbPath = database_p + database_n;
dataBase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
boolean exist = false;
try {
String dbPath = database_p + database_n;
checkDB = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
Log.v("santosh", "database error");
}
if (checkDB != null) {
Log.v("santosh", "database exist");
exist = true;
checkDB.close();
}
return exist;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
1) first put your .db file(database file ) in assets folder
2) then use below code for copy database into project package.
package com.mobidhan.databasecopy;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DataBaseCopy extends SQLiteOpenHelper{
private static String database_n;//database name
private static String database_p ;//database path
private static final int DATABASE_VERSION = 2;
private static String DatabaseName;
private SQLiteDatabase dataBase;
private final Context dbContext;
//DB_NAME is name of database which is in assets folder
//path is our package name
public DataBaseCopy(Context context,String DB_NAME,String PATH) {
super(context, DB_NAME, null, DATABASE_VERSION);
DatabaseName=DB_NAME;
database_p="/data/data/"+PATH+"/databases/";
this.dbContext = context;
database_n = DatabaseName;
// checking database and open it if exists
if (checkDataBase()) {
openDataBase();
} else
{
try {
this.getReadableDatabase();
copyDataBase();
this.close();
openDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
Toast.makeText(context, "database is created", Toast.LENGTH_LONG).show();
}
}
private void copyDataBase() throws IOException{
InputStream myInput = dbContext.getAssets().open(database_n);
String outFileName = database_p + database_n;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String dbPath = database_p + database_n;
dataBase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
boolean exist = false;
try {
String dbPath = database_p + database_n;
checkDB = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
Log.v("santosh", "database error");
}
if (checkDB != null) {
Log.v("santosh", "database exist");
exist = true;
checkDB.close();
}
return exist;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Monday, 4 February 2013
Contact Picker in android
Contact Picker one of the impotent utility which we are using many of app. with the help of this we can pick contact from our phone contact list and show/use them into our app.
1) xml layout for Contact Picker
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Contact" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick " />
</LinearLayout>
2) Activity java Code:-
public class Scheduler extends Activity implements OnClickListener {
private Button b1;
private EditText contact;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
contact=(EditText)findViewById(R.id.editText1);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==b1)
{
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri ur=data.getData();
Cursor c=managedQuery(ur, null, null, null, null);
if(c.moveToFirst())
{
String s=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setText(s);
}
}
}
}
1) xml layout for Contact Picker
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Contact" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick " />
</LinearLayout>
2) Activity java Code:-
public class Scheduler extends Activity implements OnClickListener {
private Button b1;
private EditText contact;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
contact=(EditText)findViewById(R.id.editText1);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==b1)
{
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri ur=data.getData();
Cursor c=managedQuery(ur, null, null, null, null);
if(c.moveToFirst())
{
String s=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setText(s);
}
}
}
}
Time Picker in Android
Here we see that how we use Time Picker Dialog to pick time and show in textview :-
2) XML Layout for TimePicker
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Time" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick" />
</LinearLayout>
private int hour;
private int minute;
static final int Time_DIALOG_ID = 125;
private Button b3;
private EditText time;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(this);
contact=(EditText)findViewById(R.id.editText1);
date=(EditText)findViewById(R.id.editText2);
time=(EditText)findViewById(R.id.editText3);
final Calendar c = Calendar.getInstance();
hour=c.get(Calendar.HOUR);
minute=c.get(Calendar.MINUTE);
}
@Override
public void onClick(View arg0) {
if(arg0==b3)
{
showDialog(Time_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case Time_DIALOG_ID:
return new TimePickerDialog(this, timepickerlistner,hour, minute,true);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timepickerlistner =new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
hour = arg1;
minute = arg2;
time.setText(new StringBuilder().append(hour)
.append(":").append(minute).append(" "));
}
};
}
2) XML Layout for TimePicker
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Time" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick" />
</LinearLayout>
2) Java Code For Time Pickerpublic class Scheduler extends Activity implements OnClickListener {
private int hour;
private int minute;
static final int Time_DIALOG_ID = 125;
private Button b3;
private EditText time;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(this);
contact=(EditText)findViewById(R.id.editText1);
date=(EditText)findViewById(R.id.editText2);
time=(EditText)findViewById(R.id.editText3);
final Calendar c = Calendar.getInstance();
hour=c.get(Calendar.HOUR);
minute=c.get(Calendar.MINUTE);
}
@Override
public void onClick(View arg0) {
if(arg0==b3)
{
showDialog(Time_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case Time_DIALOG_ID:
return new TimePickerDialog(this, timepickerlistner,hour, minute,true);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timepickerlistner =new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
hour = arg1;
minute = arg2;
time.setText(new StringBuilder().append(hour)
.append(":").append(minute).append(" "));
}
};
}
Date Picker in Android
In This Section we Know the how to use Date Picker Dialog in android to pick date and show in textview:
1) Layout for Date Picker Activity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Date" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick" />
</LinearLayout>
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 123;
private Button b1;
private EditText date;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
date=(EditText)findViewById(R.id.editText2);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
@Override
public void onClick(View arg0) {
if(arg0==b1)
{
showDialog(DATE_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
date.setText(new StringBuilder().append(month + 1)
.append("/").append(day).append("/").append(year).append(" "));
}
};
}
1) Layout for Date Picker Activity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Select Date" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pick" />
</LinearLayout>
2) Java Activity Code :public class Scheduler extends Activity implements OnClickListener {
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 123;
private Button b1;
private EditText date;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
date=(EditText)findViewById(R.id.editText2);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
@Override
public void onClick(View arg0) {
if(arg0==b1)
{
showDialog(DATE_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
date.setText(new StringBuilder().append(month + 1)
.append("/").append(day).append("/").append(year).append(" "));
}
};
}
Subscribe to:
Posts (Atom)