Unable to get songs according to album and artists - android

I want to show songs according to artists and albums but i am unable to query for it.
String[] projection = new String[] { MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Albums.NUMBER_OF_SONGS };
String selection = null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Audio.Media.ALBUM + " ASC";
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, null, null, null);
Can anyone tell me what's wrong with this code ?

If you are putting null in your resolver.query, you do not need to assign variables selection and SelectionArgs.
On the face of it I can not see anything wrong but you have not listed any errors or what the outcome actually is.
However this is how I do it:
public Cursor getAlbums(Context context) {
String[] dataColumns;
ContentResolver resolver = context.getContentResolver();
dataColumns = new String[]{
BaseColumns._ID,
MediaStore.Audio.AlbumColumns.ALBUM,
MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
MediaStore.Audio.AlbumColumns.FIRST_YEAR,
MediaStore.Audio.AlbumColumns.ARTIST};
String sort_order = MediaStore.Audio.AlbumColumns.ALBUM + " ASC";
Cursor acursor = resolver.query
(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
dataColumns,
null, null,
sort_order);
return acursor;
}

Related

How to get phone numbers of contact members of a given contact group in android using cursor

I have gotten the group id from cursor. Let's say groupID as '59'. I want to get all contacts from the groups where Group_ID is 59. I have manipulated projection, selection and cursor with different classes of ContactsContract but no use. Here is what my code for getting member contacts looks like.
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection22 = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
String sortOrder = null;
Cursor cursor = getContentResolver().query(uri, projection22,
selection, selectionArgs, sortOrder);
Now the cursor returns zero count. Please get me through to use the cursor in a proper way.
public void getSampleContactList(int groupID) {
contactList = new ArrayList<ConatctData>();
Uri groupURI = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID };
Cursor c = getContentResolver().query(
groupURI,
projection,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ "=" + groupID, null, null);
while (c.moveToNext()) {
String id = c
.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id }, null);
while (pCur.moveToNext()) {
ConatctData data = new ConatctData();
data.name = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
data.phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(data);
}
pCur.close();
}
}
class ConatctData {
String phone, name;
}
original post

Include only person-based contacts when querying ContactsContract.Contacts

I have a SearchView in which if I type a name, it shows a autocomplete list of names matching it. However contacts like Facebook ambulance and AL Cricket also come in these results. How to exclude such results and only get those contacts that are of actual people?
Code I am using to get display name is this :
private String getDisplayNameForContact(Intent intent) {
Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
phoneCursor.moveToFirst();
int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = phoneCursor.getString(idDisplayName);
phoneCursor.close();
return name;
}
You need to add a selection and selection arguments to your contentResolver.query.
The arguments you want are:
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
String[] selectionArgs = {"1"};
You add them like so:
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor contactsCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, selection, selectionArgs, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
The constant ContactsContract.Contacts.IN_VISIBLE_GROUP determines what subgroup of contacts you are querying.
As you can see, when you call - query(intent.getData(), null, null, null, null) - get all list data. Where is null, theare is your selections parameters. It remains only to choose the appropriate options. Try my example, and swith different values (ContactsContract.CommonDataKinds.SomethingElse).
I hope this helps.
private static Cursor allContactsQuery(Context context) {
final String[] CONTACTS = new String[]{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.LOOKUP_KEY,
};
String SELECTION = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY +
"<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1" +
" AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
final String[] SELECTION_ARGS = null;
final String SORT_ORDER = ContactsContract.Contacts.SORT_KEY_PRIMARY;
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACTS,
SELECTION,
SELECTION_ARGS,
SORT_ORDER);
return cursor;
}

Android contact search

I want to implement a contact search with simplecursoradapter. And it should behave like standard android contact search. The problem is I can't write filter right. Now I have something like this:
private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
#Override
public Cursor runQuery(CharSequence Constraint) {
ContentResolver contentResolver = getActivity().getContentResolver();
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,Uri.encode(Constraint.toString()));
String[] projection = { BaseColumns._ID, Phone.PHOTO_URI, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE };
return contentResolver.query(
uri,
projection,
null,
null,
"upper(" + Phone.DISPLAY_NAME + ") ASC");
}
};
And it works, but there is a thing. When I put in filter a letter, 'm' for example, this filter gives me contacts which phones starts with '5'. So it "cast" letters to numbers. And I don't want this. What should I do?
Here is my code snippet for searching contacts by name. Maybe you'll find something is missing:
public String getPhoneNumber(String name, Context context) {
String ret = null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
if (c.moveToFirst()) {
ret = c.getString(0);
}
c.close();
if(ret==null)
ret = "Unsaved";
return ret;
}

how to read and write Android myself contact

I am working with Android contacts, which is Android 4.0.3. In the contact app, I can create, add, edit and see myself contact in it. But when I tried to get myself contact from my app using contentResolver, it did not work. How can I read and write myself contact?
Thanks in advance.
EDIT: here is my code to get contacts
List list = new ArrayList();
Uri uri = Contacts.CONTENT_URI;
String[] projection = new String[] {
Contacts._ID,
Contacts.LOOKUP_KEY,
Contacts.DISPLAY_NAME,
Contacts.HAS_PHONE_NUMBER,
Contacts.PHOTO_ID,
Contacts.LAST_TIME_CONTACTED,
Contacts.TIMES_CONTACTED
};
String sortOrder = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
if (sortColumn != null) {
if (sortColumn.equals(Contacts.LAST_TIME_CONTACTED) || sortColumn.equals(Contacts.TIMES_CONTACTED))
sortOrder = "" + sortColumn + " COLLATE LOCALIZED DESC";
}
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, sortOrder);
while (cursor.moveToNext()){
list.add(getRecord(context, cursor));
}
return list;
} finally {
if (cursor!=null) cursor.close();
}
Try this Uri below.
Uri uri = Uri.withAppendedPath(
ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
This is the path of "Myself".

Query Contact members from a specified group?

I need to fetch the members for a specific group in android contacts.
I have the contact group names and their ID's
Can anyone provide me how to query the contacts provider for members in a particular group ?
Try this method:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
else
selection = "1) GROUP BY (" + ContactsContract.Data.CONTACT_ID;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
return getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
}
This works on Android 2.3.3 and lower, but doesn't work on Android 4+ and I don't currently know why.
UPD.
Adding custom string parameter "GROUP BY" to SQL query is denied in Android 4+, so I've founded this workaround:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
Cursor cursor = getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
MatrixCursor result = new MatrixCursor(projection);
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] { cursor.getLong(0),
cursor.getLong(1), cursor.getString(2) });
}
}
return result;

Categories

Resources