Nov 21, 2011

Print special symbols like " , / , \ . ..

Hi Every ody ,

I some case we have to print some special symbols like ' , " , / , like

that case simply add (forward slash ) \ before printing special symbol

Example :
-----------
here i want to print below message :

Hello " tutorials-Android " by sravan

then use
String str="Hello \" tutorials-Android \" by sravan";

Program :
-----------


public class Demo {
public static void main(String[] args) {
//String str="Hello\"hai\" wat dng";
String str="Hello \" tutorials-Android \" by sravan";
System.out.println(str);

}

}


out Put :
--------

Hello " tutorials-Android " by sravan

Nov 14, 2011

How to Solve OutOfMemory Error (OOM) in Android ?


When you try to decode large files on your Android device (where you have only 16MB memory available) you wil surely get an OutOfMemory exception.

We have found that if you create a temp storage and pass it to BitmapFactory before decoding might help.

So, before using BitmapFactory.decodeFile() create a byte array of 16kb and pass it to temp storage in decoding process.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);

Also you might consider passing your own outputstream that streams out to server or other storage to minimize memory consumtion during the process.

PS, do not forget to bitmapImage.recycle() after usage.

Also consider using options.inSampleSize with values other than 1. From SDK: If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 -> decodes full size; 2 -> decodes 1/4th size; 4 -> decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.

OutOfMemory exception when decoding with BitmapFactory



Google Android, Memory, and Bitmaps

Working on mobile devices forces one to make conscious decisions regarding coding choices, if for no other reason that resources are scarce (memory, screen size, bandwidth). Taking the easy route and ignoring wise mobile programming practices can take what could be a promising application and make it a disappointing user experience.

If you’ve spent any time with the Google Android SDK, and have tried to read a JPEG into a Bitmap using Media.getBitmap, you’ve almost certainly run into this little gem of an error message:

bitmap size exceeds VM budget

Unfortunately, since Android caps all applications’ VMs at 16MB in size, it only takes one or two big image reads to get you into trouble, regardless of all the garbage collection and Bitmap recycles you may try (see code snippet at the end of this post for more on that).

So, what’s a programmer to do?

Well, the only thing one can do is to read only what you need into memory. That means that you won’t be able to read in that 10MB jpeg sitting on your phone (at least, you won’t be able to read it reliably and / or repeatably) without trimming it down a bit.

The code below will do this for you; rather than calling Media.getBitmap, use this readBitmap function instead:





// Read bitmap
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,”r”);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}


The magic fairy dust in this function that allows you to trim down large bitmaps into digestible sizes is the options.inSampleSize property. inSampleSize – in this instance – takes a bitmap and reduces its height and width to 20% (1/5) of its original size. The larger the value of inSampleSize N (where N=5 in our example), the more the bitmap is reduced in size.

There’s also another coding practice that one should always use when dealing with Bitmaps in Android, and that is the use of the Bitmap recycle() method. The recycle() method frees up the memory associated with a bitmap’s pixels, and marks the bitmap as “dead”, meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.

In my projects, I have a helper function that does cleanup when I am finished using a Bitmap, named clearBitmap:

// Clear bitmap

public static void clearBitmap(Bitmap bm) {

bm.recycle();

System.gc();

}



Dealing with memory errors on Android apps seems to be the “problem child” issue that crops up the most (like too many threads on Blackberry apps – different post for a different day).

But like parenting a problem child, what’s called for is attention and intention to mitigate havoc wreaked.

Nov 11, 2011

Reading Names And Contacts In Android and Storing In Bean

import java.io.InputStream;
import profileit.utils.ContactsBean;
import profileit.utils.StaticUtils;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.WindowManager;

public class Splash extends Activity {
private final int DISPLAY_LENGTH = 2000;
static ContentResolver cr;
static Cursor cursor;
@Override
public void onCreate(Bundle si) {
super.onCreate(si);
// title bar removing
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
// call next activity specified delay time
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// clear utils if necessary
StaticUtils.sALContacts.clear();
StaticUtils.sALNames.clear();

getHome();

}

}, DISPLAY_LENGTH);
}
// gettings Contacts
public void getHome() {

cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);

if (cur.getCount() > 0) {
int count = 0;
while (cur.moveToNext()) {
// created bean for Storing data as a object
ContactsBean bean = new ContactsBean();
// getting contact id as a String
String contid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// getting contact id as a long type
long contactId = cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID));
// Log.e("iteratin started" + ++count, contid);
// fecthing contact name using contact ID allocated by OS
String name = getContacName(contactId);
if (name != null) {
// fecthing contact number using contact ID allocated by OS
String cNumber = getContactNumber(contactId);
if (cNumber != null) {
// Storing number in Bean
bean.setmPhoneNumber(cNumber);
} else {
bean.setmPhoneNumber(StaticUtils.sTstNoNumbMesg);
}
//fecthing Email using contact ID
String cEmail = StaticUtils.getContactEmail(cr, contactId);
if (cEmail != null) {
// Storing Email in Bean
bean.setmEmail(cEmail);
} else {
bean.setmEmail(StaticUtils.sTstNoEmailMesg);
}
//fecthing photo using contact ID, it returns as a stream
InputStream is = StaticUtils.getContactPhoto(cr, contactId);
if (is != null) {
// Storing photo in Bean
bean.setmPhoto(true);
} else {
bean.setmPhoto(!true);
}
if (cNumber != null || cEmail != null || is != null) {
bean.setmContact_Id(contid);
bean.setmName(name);
// storing Bean object into Arraylist
StaticUtils.sALContacts.add(bean);
// storing names in bean
StaticUtils.sALNames.add(name.toLowerCase());
// Log.e("contact name",StaticUtils.sALNames.toString());
// Log.e("contacts",StaticUtils.sALContacts.toString());
} else {
// Log.e("No contact for",name);
}
} else {
// Log.e("contact name may", "be null");
}
// Log.e("iteratin over" + count, contid);
}
}
}

// fecthing name using contact ID
public String getContacName(long contactId) {
// formarting URI for getting name
cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data.DATA1 },
Data.CONTACT_ID + "=" + contactId + " and " + Data.MIMETYPE
+ "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null,
null);
if (cursor == null) {
// Log.e("cursor ContacName", "is null");
return null;
} else {
try {
// Log.e("cursor ContacName", "is not null");
if (cursor.moveToFirst()) {
String data = cursor.getString(0);
if (data != null) {
// Log.e("data ContacName", "obj" + data);
return data;
} else {

// Log.e("data ContacName", "is null");
}
} else {
// Log.e("cursor move first ContacName", "failed");
}
} finally {
cursor.close();
}

}

return null;
}

// fecthing number using contact ID
public String getContactNumber(long contactId) {
// formarting URI for getting name
cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data.DATA1 },
Data.CONTACT_ID + "=" + contactId + " and " + Data.MIMETYPE
+ "='" + Phone.CONTENT_ITEM_TYPE + "'", null, null);
if (cursor == null) {
// Log.e("cursor getPhone", "is null");
return null;
} else {
try {
// Log.e("cursor getPhone", "is not null");
if (cursor.moveToFirst()) {
String data = cursor.getString(0);
if (data != null) {
// Log.e("data getPhone", "obj" + data);
return data;
} else {
// Log.e("data getPhone", "is null");
}
} else {
// Log.e("cursor move first getPhone", "failed");
}
} finally {
cursor.close();
}
}
return null;

}

}

Nov 10, 2011

How to call Android contacts list?

Hi this is very import concept to Read Existing Contacts from Your Emulator or Device

There are three steps to this process.

1) Permissions

Add a permission to read contacts data to your application manifest.

 android:name="android.permission.READ_CONTACTS"/>

2) Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACTin this example). This will cause Android to launch an Activity that's registered to support ACTION_PICKon the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override
public vo
id onActivityResult(int reqCode, int resultCode, Intent data) {
sup
er.onA


ctivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}

Home Screen If No Contacts Contacts










For Online Training :

For Full Source Cliked Here