Getting audio file path or URI from mediastore

15,333

So I Figured out the problem in the code, Everything was correct except the uri generation part, I had to add "file:///" and it worked. So I Replaced

Uri uri= Uri.parse(song.getPath());

with

Uri uri= Uri.parse("file:///"+song.getPath());

and it worked like charm! Hope someone else will find this useful!

Share:
15,333
Swapnil Gupta
Author by

Swapnil Gupta

I am a Computer Science Student Currently Pursuing My B.tech. I Love To Code and Love to Learn New Things.

Updated on June 04, 2022

Comments

  • Swapnil Gupta
    Swapnil Gupta almost 2 years

    I am making an application which lists all the songs on the device onto the screen and clicking on them opens a sharing intent where the songs / audio file can be shared across various devices through different method like Bluetooth, WhatsApp etc. But I am not able to get the file path or location of the audio file from the mediastore.

    This is how I am getting my songs-

    public void getSongList() {
        //retrieve song info
        ContentResolver musicResolver = getActivity().getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        //iterate over results if valid
        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            int albumId = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM_ID);
            int data= musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
            int albumkey=musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                long thisalbumId = musicCursor.getLong(albumId);
                String thisdata= musicCursor.getString(data);
                String AlbumKey = musicCursor.getString(albumkey);
                tempSongList.add(new Song(thisId, thisTitle, thisArtist, thisalbumId, thisdata, AlbumKey));
    
            }
            while (musicCursor.moveToNext());
    
        }
    }
    

    I am storing all songs in the arraylist of a class Song which is below-

    package com.musicplayer;
    
    import android.content.ContentUris;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.os.Parcel;
    import android.os.ParcelFileDescriptor;
    import android.os.Parcelable;
    
    import java.io.FileDescriptor;
    import java.io.FileNotFoundException;
    import java.io.Serializable;
    
    /**
     * Created by swappnil on 19-06-2015.
     */
    public class Song  implements Parcelable {
    private long id;
    private String title;
    private String artist, data, albumkey;
    private long alid;
    
    public Song(long songID, String songTitle, String songArtist, long albumID, String thisdata, String AlbumKey) {
        id=songID;
        title=songTitle;
        artist=songArtist;
        alid=albumID;
        data=thisdata;
        albumkey=AlbumKey;
    
    }
    public Song(){
    
    }
    public long getID(){return id;}
    public String getTitle(){return title;}
    public String getArtist(){return artist;}
    public long getAlbumID(){return alid;}
    public String getPath(){return data;}
    public String getAlbumKey(){return albumkey;}
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        dest.writeString(artist);
        dest.writeLong(alid);
        dest.writeLong(id);
        dest.writeString(data);
        dest.writeString(albumkey);
    }
    public static final Parcelable.Creator<Song> CREATOR = new Parcelable.Creator<Song>() {
        public Song createFromParcel(Parcel in) {
            Song song = new Song();
            song.title = in.readString();
            song.artist = in.readString();
            song.alid = in.readLong();
            song.id = in.readLong();
            song.data= in.readString();
            song.albumkey=in.readString();
            return song;
        }
    
        public Song[] newArray(int size) {
            return new Song[size];
        }
    };
    }
    

    Now when the user clicks on certain song what I am doing to share it is this-

    String sharePath = currSong.getPath();
        Uri uri = Uri.parse(sharePath);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("audio/*");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(share, "Share Sound File"));
    

    Where currSong is the song on which user has clicked. Where am I wrong.