android - Getting audio file path or uri from mediastore -
i making application lists songs on device onto screen , clicking on them opens sharing intent songs / audio file can shared across various devices through different method bluetooth, whatsapp etc. not able file path or location of audio file mediastore.
this how getting 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 on 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 list { 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 storing songs in arraylist of class song 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 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 user clicks on song doing share 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 song on user has clicked. wrong. suggestions please.
so figured out problem in code, correct except uri generation part, had add "file:///" , worked. replaced
uri uri= uri.parse(song.getpath());
with
uri uri= uri.parse("file:///"+song.getpath());
and worked charm! hope else find useful!
Comments
Post a Comment