How to get song title? Android - android

I have song on SD card and I'm trying to get title of that song:
File f = new File(filename);
String[] projection = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION};
Cursor musiccursor = managedQuery(Uri.fromFile(f),
projection, null, null, null);
musicTV.setText(musiccursor.getString(3));
but in last line i've got NullPointerException.
Thanks for any advice.

if you are using a cursor, when you are getting some content you have to move the cursor to the first element, because the cursor is pointing before the first element.
Try using:
Cursor musiccursor = managedQuery(Uri.fromFile(f), projection, null, null, null);
cursor.moveToFirst();
musicTV.setText(musiccursor.getString(3));

Have you seen this which gets you 90% there, then this will get you the rest of the way.
Query Device
Init Cursor
Traverse Cursor
The hard part is understanding the abstract nature of columns. Just remember it's a database table and can be visualized as a spreadsheet. DON'T FORGET CATCH STATEMENTS!

Related

MediaStore to list from specific path

I am trying to get list of mp3 files from a particular folder.
As of now, I am using contentResolver.query for getting the music from my phone.
Like this:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
ContentResolver contentResolver = context.getContentResolver();
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);
I am looking for solution to scan only a particular folder. How do I do this using contentResolver?
Thanks!
Finally figured it out myself. Hope it will help others.
Instead of this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
I used this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%MyMusicFolder%"}, null);

Android: query for albums gives duplicates

I'm querying the contentresolver in order to list all the albums on the device. However I have losts of duplicate. Is it a problem of my query or of the android database?
Maybe is it because I'm getting albums from a songs' table?
String[] projection = {
MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST
};
String selection = null;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
Thank you

sql where clause for managedQuery android method

I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );

How to query for songs in playlists on Android SDK?

So my question is how to query for playlist content. I can make a query to display all the playlists, but how to make a query to display songs in a specific playlist.
Thanks
Okay, so I have manged to put the code together myself. So this is it :
String[] proj = { MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
c = getContentResolver().query( MediaStore.Audio.Playlists.Members.getContentUri("external",playlistID),
proj,
null,
null,
null);
startManagingCursor(c);

Can't get a managedQuery with selection to work

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.

Categories

Resources