Get the DISPLAY_NAME using DATA in android MediaStore

737 views Asked by At

I am building a music player app in android. On clicking a listItem the corresponding song will play and the name of the song will be displayed above the seek bar in a TextView with id "selectedItem". But the data binding is done in MediaCursorAdapter.class which will return the MediaStore.MediaColumns.DATA as a string to the method onListItemClick() of the MainActivity.java Class. Here the name of the current song is set to the String. But this shows the entire path of the song in the App which looks pretty bad. Is there a way to get the DISPLAY_NAME in my MainActivity.java class or to somehow shorten this path so that it doesn't look so bad in the app?

MainActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    currentFile = (String) v.getTag();
    startPlay(currentFile);
}

MediaCursorAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //TextView title = (TextView) view.findViewById(R.id.title_name);
    TextView displayName = (TextView) view.findViewById(R.id.display_name);
    TextView duration = (TextView) view.findViewById(R.id.duration);
    String nameOfTheSong = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
    nameOfTheSong = nameOfTheSong.substring(0,nameOfTheSong.length()-4);
    displayName.setText(nameOfTheSong);
    //title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
    long durationInMS = Long.parseLong(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
    double durationInMin = ((double)durationInMS/1000.0)/60.0;
    BigDecimal bigDecimal = new BigDecimal(durationInMin);
    durationInMin = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
    duration.setText(""+durationInMin);
    //SETTING THE PATH WHICH IS THEN USED IN onListItemClick OF THE MAINACTIVITY.JAVA CLASS
    view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}

Github link: https://github.com/vishwabhat19/PlayMusic.git

1

There are 1 answers

0
Theo On

You do not show how you get your cursor but you could use:

    MediaStore.Audio.Media.TITLE

or if you have the full path

 String trackname = fullpath.substring(fullpath.lastIndexOf("/") + 1, fullpath.length());