I basically can not accomplish this for over 2 weeks now.
I have posted a numerous amount of code over a few questions yet most of them got ignored so I won't flood this question with more code of my own that won't even be read.
How do I search the MediaStore with "LIKE" attributes using a String?
E.G. I type in Shoot To Thrill, I receive song ID with this code:
if(cursor.moveToFirst()){
while(cursor.moveToNext()){
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
test.setText(title +" " + artist + " " + id);
}
}
Here is something to begin with:
String[] projection = {
BaseColumns._ID,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Media.TITLE
}
Cursor cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, //uri
projection, //projection
//i dont know what to enter,
//i dont know what to enter,
MediaStore.Audio.Media.TITLE);
It's just normal SQL hiding behind the scenes. Normal LIKE operations should work just fine. You can use MediaStore.Audio.Media.TITLE + " LIKE \"%thrill%\"" just like any other SQL query.
String[] projection = { BaseColumns._ID,
MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Media.TITLE };
String where = MediaStore.Audio.Media.TITLE + " LIKE ?";
String[] params = new String[] { "%life%" };
Cursor q = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, where, params, MediaStore.Audio.Media.TITLE);
try {
while (q.moveToNext()) {
Log.e("song", q.getString(1) + " " + q.getString(2));
}
} finally {
q.close();
}
Related
I want take data from a row with two parameters, and only return the first.
This is my code:
String[] projection = { VehiculoContentProv.Consumos.ID_VEH_C,
VehiculoContentProv.Consumos.ID_CALCULO_KL,
VehiculoContentProv.Consumos.ID_CALCULO_MG};
String selection = "_idv =" + idV1 + " AND " + idV2;
cursorLoader = new CursorLoader(this, VehiculoContentProv.CONTENT_URI_CONSUMO,
projection,
selection, null, null);
thanks a lot
I have set up the cursor but I am still struggling with actually searching it. How do I search for the track name? How do I get its ID so I can pass it to mediaplayer? I have tried different things but didn't succeed.
Basically, I want to know how to search the mediastore for a certain string (string songToPlay in this case), get the results, get the ID of the best match and pass it to my mediaplayer service to play it in the background. The only thing I am asking you is how to get the ID though.
This is the code I am using:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] songToPlay = value.split("play song");
String[] searchWords = songToPlay[1].split(" ");
String [] keywords = null;
StringBuilder where = new StringBuilder();
where.append(MediaStore.Audio.Media.TITLE + " != ''");
String selection = where.toString();
String[] projection = {
BaseColumns._ID,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Media.TITLE
};
for (int i = 0; i < searchWords.length; i++) {
keywords[i] = '%' + MediaStore.Audio.keyFor(searchWords[i]) + '%';
}
Cursor cursor = this.managedQuery(uri, projection, selection, keywords, MediaStore.Audio.Media.TITLE);
if (cursor.moveToFirst()) {
do{
test.setText(cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
}while(cursor.moveToNext());
}
I'm very confused and most of this code is from the internet. I have been trying to do this for 2 days now, that's why I'm asking here. I've done hours of trying by myself.
edit: Current code. How am I supposed to find the location - music ID or whatever I need to play that song from the cursor?
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String songToPlay = value.replaceAll("play song", "").trim();
int music_column_index;
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Media.TITLE
};
#SuppressWarnings("deprecation")
Cursor cursor = this.managedQuery(uri,
null,
MediaStore.Audio.Media.TITLE + "=?",
new String[]{songToPlay},
MediaStore.Audio.Media.TITLE + " ASC");
This will search the MediaStore for all songs with title equal to the value of your songToPlay string.
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
MediaStore.Audio.Media.TITLE + "=?",
new String[]{songToPlay},
MediaStore.Audio.Media.TITLE + " ASC");
You can then of course query the cursor to get the ID (and any other column) of any results.
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
// Whatever else you need
}
However this will require an exact match, you may like to use the LIKE operator for a more fuzzy match.
I was just curious about what the MediaStore.Images.Media.MINI_THUMB_MAGIC column contains. Does it have anything to do with the image's thumbnail? Thank you.
See MediaStore.Images.ImageColumns from the Android developers ref. for the doc -- which says it returns "the mini thumb id".
Also see Displaying Thumbnail Photos on Map for how to use this ID:
long thumbId = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.MINI_THUMB_MAGIC));
String[] args = new String[]{String.valueOf(thumbId)};
Cursor ct = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Thumbnails._ID + "= ?", args, null);
MINI_THUMB_MAGIC returns a random number and you can not get thumbnail id from this. Use MediaStore.Images.Media._ID, it is the same number as MediaStore.Images.Thumbnails.IMAGE_ID
ArrayList<String> ids; // ArrayList of MediaStore.Images.Media._ID
String selection = MediaStore.Images.Thumbnails.IMAGE_ID + " IN (" + TextUtils.join(",", ids) + ")";
Cursor cursor2 = resolver.query(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
new String[]{{MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Thumbnails.IMAGE_ID}},
selection,
null,
null);
i have an HTC legend.In its phone contacts it shows 4-5 contacts.But when i query it in my application it shows nearly 45 contacts.I think its gmail contacts.
String[] Projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.STARRED};
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, Projection, null, null, null);
I am getting all the details with photo as well in a listview. Now i need to get particular contact details like all his phone numbers,emails,photo etc.For this i am writing this query
final String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.IS_PRIMARY,
};
final Cursor phone = managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{contactId},
null);
> if(phone.moveToFirst()) {
> final int contactNumberColumnIndex =
> phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
> final int contactTypeColumnIndex =
> phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
i am getting all the details of the phone contact but not all other contacts.
i have read that all these details are stored in shared table(ContactsContract.Data.CONTENT_URI), but i am not getting exactly what to do go get this done.
Can someone help me figure out how to do this.
Thanks
You can do something like:
getContentResolver().query(
RawContactsEntity.CONTENT_URI,
// projection
new String[] {
BaseColumns._ID,
RawContactsEntity.MIMETYPE,
// DATA columns are interpreted according to MIMETYPE
// Project only union of data columns needed
RawContactsEntity.DATA1,
RawContactsEntity.DATA2,
RawContactsEntity.DATA3,
RawContactsEntity.DATA4,
RawContactsEntity.DATA7,
RawContactsEntity.DATA9,
RawContactsEntity.DATA10,
},
// selection
RawContactsEntity.DELETED + "<>1 AND " +
RawContactsEntity.MIMETYPE + " IN ('" +
StructuredName.CONTENT_ITEM_TYPE + "','" +
Email.CONTENT_ITEM_TYPE + "','" +
Phone.CONTENT_ITEM_TYPE + "','" +
StructuredPostal.CONTENT_ITEM_TYPE + "','" +
Organization.CONTENT_ITEM_TYPE + "')",
// selection params
null,
// sorting
null);
Next iterate through cursor, and first read MIME type:
final String mimeType = cursor.getString(cursor.getColumnIndex(
RawContactsEntity.MIMETYPE));
And depends on MIME type read corresponding data kind:
if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
readNameData(cursor);
} else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
addEmailData(cursor);
}
and so on.
i have the following code to get contacts from content provider
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.PHOTO_ID };
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
columns, null, null, null);
and i use this one to get the emails for a specific contact by their id:
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + contact.getContactId(), null, null);
my current implementation passes every row in the cursor and aquires its emails and stores them in an arrayList of java objects.
what i was wondering if it was possible to do is just query the content provider and return a cursor of just contacts with ids/name etc that have an email address listed.
this way has a long waiting period for getting the contact list. i am using this list for a list adapter. if i can get only the contacts that have an email i can use a cursor adapter in my list.
Is something like this possible? how can i speed up the process?
#CapDroid
Fixed working code from DArkO's post:
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
Your cursor will have essential IDs as well as names and email addresses. The performance of this code is great because it requests few columns only.
I solved this one, here is how its done:
UPDATE
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1" + " ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;
return mContent.query(Email.CONTENT_URI,
PROJECTION, filter, null, order);