I'm trying to get name of Contact List from phone's contacts list, I have error on first line of try: ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Any help?
ContactFragment.java
private String refreshData() {
String namedata = "";
try {
/**************************************************/
ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + namedata + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(namedata));
phones.close();
}
}
}
Try Like This :
Set this tag in manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Try this code :
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//...
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
Related
I am using the code below to access the phone numbers and the names of the contact. I want to add to this function a way to also add the photo of the contact.
void loadContacts() {
ContentResolver contentResolver=getContentResolver();
Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if(cursor.getCount() > 0) {
while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
make_contact(name,phoneNumber);
}
cursor2.close();
}
}
}
cursor.close();
}
For Bitmap Image,
private Bitmap retrieveContactPhoto(String contactID) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
assert inputStream != null;
inputStream.close();
return photo ;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
By using contact Id you can retrieve bitmap image and set to imageview.
try this
public Uri getPhotoUri() {
try {
Cursor cur = this.ctx.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
+ ContactsContract.Data.MIMETYPE + "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
.parseLong(getId()));
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
now set to image
Uri u = objItem.getPhotoUri();
if (u != null) {
mPhotoView.setImageURI(u);
} else {
mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}
I am using
Uri uriSms = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor cursor = getContentResolver().query(uriSms, null,null,null,null);
cursor.moveToLast();
while (cursor.moveToPrevious())
{
String recipient_ids= cursor.getString(cursor.getColumnIndex("recipient_ids"));
String body = cursor.getString(cursor.getColumnIndex("snippet"));
}
to get a list of sms conversation.
The "recipient_ids" returns some value Like 302, 301 259 etc. What I want is a function where i will pass the "recipient_ids" as perameter and it will return the display name of the contact (If available else the Number)
These methods help you to achieve what you want:
getContactByRecipientId - to get contact number by recipientId.
getContactbyPhoneNumber - to get display name by phone number.
public String getContactByRecipientId(long recipientId) {
String contact = "";
Uri uri = ContentUris.withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId);
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor.moveToFirst()) {
contact = getContactbyPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}
return contact;
}
public String getContactbyPhoneNumber(String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NORMALIZED_NUMBER };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
String name = null;
String nPhoneNumber = phoneNumber;
try {
if (cursor.moveToFirst()) {
nPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NORMALIZED_NUMBER));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
} finally {
cursor.close();
}
if(name != null){ // if there is a display name, then return that
return name;
}else{
return nPhoneNumber; // if there is not a display name, then return just phone number
}
}
Please check this code
private void fetchInbox() {
Uri inboxURI = Uri.parse("content://sms/inbox");
String[] reqCols = new String[]{"_id", "address", "body"};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber}, 0);
}
I have the following code to load all contacts with their pictures
public static void getAllContactWithNumberAndNameAndPhoto(Context context,
ArrayList<ContactInfo> mContactList, boolean starred) {
ContentResolver cr = context.getContentResolver();
Cursor cur = null;
if (starred == true) {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"starred=?", new String[] { "1" }, null);
} else {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
}
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactInfo item = new ContactInfo();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
String starredValue = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.STARRED));
boolean isFav = false;
if (starredValue.equals("1"))
isFav = true;
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phoneNo = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
}
pCur.close();
// if (photo != null) {
//
// item.setPhoto(photo.toString());
// }
item.setName(name);
item.setFavorite(isFav);
item.setRecent(false);
mContactList.add(item);
}
}
cur.close();
}
}
and the following code to load contact picture Uri
public static Uri getPhotoUriFromID(Context context, String id) {
try {
Cursor cur = context
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ id
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null;
}
} else {
return null;
}
cur.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
every thing is working fine but too slow about 40 second for 1000 contact, when I comment the part of loading image it takes 18 sec, so how can I load image uri in the same query of loading contact to minimize the waiting time of the user.
For displaying the images of contacts you can use Universal Image Loader library. It's great for handling multiple images. You can use a default image to display initially and as soon as the contact's image is loaded the library will display that image.
This is how you display image in this library:
ImageLoader.getInstance().displayImage(img, imageView, options);
where img is the image, imageView is the ImageView where you want to display that image, options is the object of DisplayImageOptions of UniversalImageLoader library.
I have problem in a database that contains the data that already exists, but the data does not appear in the textbox. please help
this is data from Form.java
String modul;
String itemreg;
modul = dataSource.getModule(assignmentid,orderid,productmoduleid,productdetailid);
//itemreg = dataSource.getItemreg(productmoduleid, productdetailid);
if(Globals.modul==null){
Globals.modul=modul;
}
String namamodul;
namamodul=dataSource.getModul1(productmoduleid);
edModul.setText(namamodul);
and this is dbdatasource.java
public String getModule(String assignmentid, String orderid, String productmoduleid, String productdetailid){
Cursor cursor = database.query(DBHelper.ASSIGNMENT_ORDER_DETAIL,allAssignemntOrderDetail, DBHelper.ASSIGNMENT_ID +"= 'assgn1' and "+DBHelper.ORDER_ID +"= 'ord1'", null, null, null, null);
cursor.moveToFirst();
AssignmentOrderDetail = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_ID));
cursor.close();
return AssignmentOrderDetail;
}
public String getModul1(String productmoduleid){
String productmodul ;
Cursor cursor = database.query(DBHelper.PRODUCT_MODULE,allProductModule, DBHelper.PRODUCT_MODULE_ID +"='"+Globals.modul+"'", null, null, null, null);
cursor.moveToFirst();
productmodul = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
cursor.close();
return productmodul;
}
try if this could help:
try{
Cursor cursor =
database.query(DBHelper.ASSIGNMENT_ORDER_DETAIL,allAssignemntOrderDetail,
DBHelper.ASSIGNMENT_ID +"= 'assgn1' and "+DBHelper.ORDER_ID +"= 'ord1'",
null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
AssignmentOrderDetail =
cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_ID));
}
return AssignmentOrderDetail;
}finally {
cursor.close();
}
Try with below code:
Cursor cursor = null;
String TABLE_NAME = "yourTableName";
String[] allColumns = {"coloumn1","coloumn2","coloumn3"};
String whereClause = "coloumn1"+"=?";
String[] whereArgs = new String[] {method_parameter};
Like below:
private Cursor getData(String param_id)
{
try{
sqliteDb = appDb.getWritableDatabase();
if(sqliteDb.isOpen())
{
cursor = sqliteDb.query(TABLE_NAME, whereClause, whereArgs);
}
}catch(Exception ex)
{
b = false;
ex.printStackTrace();
}
return cursor;
}
I need to get the name, email and phone number from each contact of my contacts list.
The problem is I can get name and phone number, but not the email. I'm getting the phone number instead of email.
Here is my code:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
ContactItem objContact = new ContactItem();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
objContact.setEmail(email);
list.add(objContact);
}
phones.close();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
//May want to get basic info here like name, phone
//Example:
//Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
//while (phones.moveToNext()) {
// String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
// Log.i("phone", phoneNumber);
//}
//phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Source and Reference:
How to read contacts on Android 2.0
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
String phoneNumber = null;
String name = null;
String emailAddress = null;
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
Cursor cursor = null;
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// May want to get basic info here like name, phone
// Example:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phone", phoneNumber);
}
phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
while (emails.moveToNext()) {
emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Log.i(DEBUG_TAG, "Got a contact result: " + name + emailAddress + "Phone Number is :- " + phoneNumber);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}