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);
}
Related
How to get the real path of multiple selected pictures from gallery stocked in an array? I was searching and I just found how to get the real path of one picture:
public static String getFileNameByUri(Context context, Uri uri)
{
String fileName="unknown";//default fileName
Uri filePathUri = uri;
if (uri.getScheme().toString().compareTo("content")==0)
{
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst())
{
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//Instead of "MediaStore.Images.Media.DATA" can be used "_data"
filePathUri = Uri.parse(cursor.getString(column_index));
fileName = filePathUri.getLastPathSegment().toString();
}
}
else if (uri.getScheme().compareTo("file")==0)
{
fileName = filePathUri.getLastPathSegment().toString();
}
else
{
fileName = fileName+"_"+filePathUri.getLastPathSegment();
}
return fileName;
}
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'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();
}
}
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 am trying to add the names of songs from an sd card on the phone to a list view but i am getting a null pointer exception...
private void updateList() {
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
Cursor tempCursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj,
null,
null,
null);
int col_index = -1;
int numSongs = tempCursor.getCount();
while (tempCursor.moveToNext()) {
col_index = tempCursor.getColumnIndexOrThrow(
MediaStore.Audio.Artists.ARTIST);
songname.add(tempCursor.getString(col_index));
}
ArrayAdapter<String> songss = new ArrayAdapter<String>(
this, R.id.songs, songname);
setListAdapter(songss);
}
I am just simply get songs data from sdcard as follows...It may helpful for you
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if(cursor.moveToFirst())
{
for(int j=0;j<cursor.getCount();j++)
{
int ALBUM_ID = cursor.getInt((cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID)));
String album_url = null;
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID);
album_url = uri.toString();
ContentResolver res = this.getContentResolver();
// Album
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM));
String year = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR));
// String year = cursor.getString(cursor
// .getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS));
// artist
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST));
// displayname
String DisplayName = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
// title
String Title = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.TITLE));
}