Get path of song from SD card in android

13,955

Get path and song Name from SD Card. You can find the path of the song from MediaStore.

The Media provider contains meta data for all available media on both internal and external storage devices.

private String[] STAR = { "*" };

public void ListAllSongs() 
    { 
        Cursor cursor;
        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        if (isSdPresent()) {
            cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null);

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        String songname = cursor
                                .getString(cursor
                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        int song_id = cursor.getInt(cursor
                                .getColumnIndex(MediaStore.Audio.Media._ID));

                        String fullpath = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DATA));

                        String albumname = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM));

                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        }
    }


public static boolean isSdPresent() 
{
    return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
}
Share:
13,955
Zankhna
Author by

Zankhna

Enthusiastic Software Engineer interested in developing Mobile and Web application that can transform Visualization to the Reality.

Updated on June 04, 2022

Comments

  • Zankhna
    Zankhna almost 2 years

    In my android application I want to fetch song from SD card but I am not able to get the path of that particular file.I am using android api level 7 which doesn't support following method.

    Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_MUSIC);
    

    I have also tried following code :

    path = Environment.getExternalStorageDirectory();
    

    but I don't know how to specify path of music file.Please suggest some solution.Thanx.

  • Chirag
    Chirag over 11 years
    May I Know the Reason for Down Vote Please ?
  • Chirag
    Chirag over 11 years
    Its retrieve songs from sdcard ... From any folder
  • Lalit Poptani
    Lalit Poptani over 11 years
    what is MusicUtils? simply copy and paste from your working projects?
  • Chirag
    Chirag over 11 years
    BTW it is one class in which i declare isSdPresent() function .
  • Lalit Poptani
    Lalit Poptani over 11 years
    I would be better from next time if you just post the clear and answer expected from the user instead of complete wall of code.