0

I'm making a music player & here is the scenario:

I deleted a song from the directory which is in the current playlist and is about to be get played.(i.e. the song actually no longer exist in the phone)

But when player tries to play the song, it throws an error, catches it and plays the next song. Simple & it is working.

However the problem is:

I upload the deleted song in the phone again on its previous place.

But when I try to access it using Android database curson using MediaStore.Audio, it still thinks that the song does not exist and throw the same error.

Below code is throwing android.database.CursorIndexOutOfBoundsException

Please suggest what to do?

Do I need to update some flags through which my application takes updated state of phone memory and able to get the song, or any thing else?

String quesryString = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0 + " AND "
        + MediaStore.Audio.Media.DATA + " = \"" + trackData + "\"";
// trackData is the name of the mp3 file & is coming right
String[] projection = { MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
        MediaStore.Audio.Media.DURATION };

Cursor cursor = context.getContentResolver().query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
        quesryString, null, null);
cursor.moveToFirst();
String songTitle =  cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
reiley
  • 3,759
  • 12
  • 58
  • 114
  • 1
    You need to use `cursor.moveToFirst()` before calling `cursor.getString(...)`. The reason is that a `Cursor` will always be set to position -1 after a query and the first record is at position 0. – Squonk Nov 25 '12 at 20:40
  • sorry... I didnt added it in code above...but i'm using moveToFirst()....and still getting this issue. Snippet Updated. – reiley Nov 26 '12 at 03:35
  • 1
    Look at the accepted answer to this question http://stackoverflow.com/questions/5250515/how-to-update-the-android-media-database it suggests forcing the `MediaScanner` to run. Hopefully it will help. – Squonk Nov 26 '12 at 03:58

0 Answers0