0

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.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
user1483208
  • 385
  • 5
  • 24
  • 2
    read cursor documentation, to begin with – njzk2 Dec 07 '12 at 16:23
  • where is `musiccursor.getColumnIndex(MediaStore.Audio.Media.TITLE)`? You've assumed that index should have value 3. This is bad assumption and event if it would be good assumption then in java index starts from 0 not from 1. – Marek R Dec 07 '12 at 16:31
  • Personally, I used null in my projection (so I get all columns) until I figured out how to do it. Then I reduced my query content. – Eric Fossum Dec 07 '12 at 16:38
  • Hey can you share the answer if possible? – SAVVY Aug 25 '17 at 23:12

2 Answers2

0

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));
Tooroop
  • 1,824
  • 1
  • 20
  • 31
0

Have you seen this which gets you 90% there, then this will get you the rest of the way.

  1. Query Device
  2. Init Cursor
  3. 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!

Community
  • 1
  • 1
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50