I am Using Android's builtin Content Provider android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI.
Now I have
String[] sarray = {"a" , "b" , "c" , "d"};`
String[] projection = {MediaStore.MediaColumns._ID , MediaStore.MediaColumns.DATA};
String selection = ?????? // My qestion
cursor = getContentResolver().query(uri, projection, null,
null , order);
So how do I write my cursor line so that I select All BUT NOT saray FROM MediaStore.MediaColumns._ID column.
Try this:
String[] sarray = {"a" , "b" , "c" , "d"};
String[] projection = {MediaStore.MediaColumns._ID , MediaStore.MediaColumns.DATA};
String selection =""+MediaStore.MediaColumns._ID+" !=? AND "+MediaStore.MediaColumns._ID+"" +
" !=? AND "+MediaStore.MediaColumns._ID+" !=? AND "+MediaStore.MediaColumns._ID+" !=?";
cursor = getContentResolver().query(uri, projection, selection,
sarray , order);
Related
I have a requirement when I need to get the Contact name of the selected numbers from the ANDROID_CONTACTS.
I can get the name of a particular number using ->
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return;
}
How can I pass a list of numbers and get their respective names?
Here's the naive approach:
String[] phones = new String[] { "(212) 555-1111", "(212) 555-2222", "(212) 555-3333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = phones;
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
DatabaseUtils.dumpCursor(cur); // dumps the cursor to logcat
However, this will only work if you enter the phone number in exactly the same format as it is stored in the ContactsContract DB under NUMBER.
The nice thing about the PhoneLookup.CONTENT_FILTER_URI API you've used is that the format of the number doesn't matter, it'll find it in the DB in any format you use.
To replicate something like that, you can try using another field called NORMALIZED_NUMBER which should always hold the number in a specific format called E164.
So you can run a query like this:
String[] e164_phones = new String[] { "+12125551111", "+12125552222", "+12125553333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = e164_phones; // all phones here must be in e164 format
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
You'll probably need a way to convert a phone number to E164 format for this code, check out this answer then.
I am implementing an AutocompleteView to search phone numbers. The code is working fine except in some conditions.
My Code :
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?";
String[] selectionArgs = new String[]{"%" + charSequence.toString() + "%"};
Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This code is working fine where there is no space in the phone numbers. For example if i enter '123' in my autocompleteView, it is able to find phone numbers like '9123456789' or '8283929383' but it is not able to find numbers '9123 456 789' or '912 3456 789'
I even tried implementing this with ContactsContract.PhoneLookup API but with this, it didnt work at all.
Code with ContactsContract.PhoneLookup API:
String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.TYPE};
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(charSequence.toString()));
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, ContactsContract.PhoneLookup.DISPLAY_NAME + " ASC");
Any help is appreciated.
Instead of using ContactsContract.CommonDataKinds.Phone.NUMBER, use ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER (this return phone numbers in E164 format)
Hi all I am trying to get contact information from the contact database using ContentResolver with these field want to get name, number, FORMATTED_ADDRESS, PHOTO
details for a contact in one single query.
So basically I need to make 3 queries per contact to obtain these
details.
What I want to know is that, is there a simpler and more efficient way
to achieve what this.
but using the below code i am getting exception.
java.lang.IllegalArgumentException: Invalid column data1
Can any body help me for finding the solution for the same.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.Photo.PHOTO};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
Maybe problem will be at selection. Replace yours method with mine.
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.Photo.PHOTO};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
String[] selectionArgs = { String.valueOf(1) };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
You should always use parametrized statements. Your approach is dangerous. And your URI was bad.
Here you go
Replace
ContactsContract.Contacts.CONTENT_URI;
with
ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
In my Android program, I'm trying to use a managedQuery with a selection argument, but I get an Sql exception.
07-26 19:30:44.364: INFO/Database(1163):
sqlite returned: error code = 1, msg = near ".": syntax error
Here is my example code
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.IS_MUSIC };
// Example without selection, works
String selection = null;
Cursor musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
// Example with selection, does not work
selection = "MediaStore.Audio.Media._ID = 1";
musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
Thanks, Niclas
The selection should be initialized like this:
selection = MediaStore.Audio.Media._ID+" = 1";
If you include the MediaStore.Audio.Media._ID in quotes the constant name is sent to SQLite to evaluate instead of actual column name _id.
I've got an android app using a local sqlite database.
private SQLiteDatabase mDb;
when I run this query I get my Cursor over rows with pid equal to id, as desired:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
when I run the following query, aiming to get that same result set, ordered by pid I get "android.database.sqlite.SQLiteException: datatype mismatch"
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");
Any ideas?
It looks like you got just a little mixed up. According to the SQLiteDatabase.query documentation, the last argument is the LIMIT clause. The second to last is the ORDER BY clause.
Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy, // <-- ORDER BY
String limit)
EDIT
But, there is also another SQLiteDatabase.query where ORDER BY would be last
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
This worked for me
String filter = MySQLiteHelper.JOB_ID + "=" + Integer.toString(jobID);
String orderBy = MySQLiteHelper.LOG_TIME + " DESC";
Cursor cursor = database.query(MySQLiteHelper.LOG_TABLE_NAME, logTableColumns,
filter, null, null, null, orderBy);
KEY_PID + " = " + "'" + id + "'"
Since Orderby is the second last parameter in the query;
your query would be like this
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
If I correctly understood your problem, try this.
String[] columns = new String[] {KEY_PID, KEY_TID};
String where = KEY_PID + " = " + Integer.toString(id);
String orderBy = KEY_PID + " DESC";
Cursor cursor = mDb.query(PT_TABLE, columns, where, null, null, null, orderBy);