从mediastore获取音频文件路径或URI

时间:2015-07-02 07:35:07

标签: android audio share mediastore

我正在创建一个应用程序,将设备上的所有歌曲列在屏幕上并点击它们打开一个共享意图,其中歌曲/音频文件可以通过不同的方式通过各种设备共享,如蓝牙,WhatsApp等。 但是我无法从mediastore获取音频文件的文件路径或位置。

这就是我收听歌曲的方式 -

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());

    }
}

我将所有歌曲存储在一首类歌曲的歌词中,该歌曲位于 -

之下
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];
    }
};
}

现在,当用户点击某首歌时,我正在分享的内容就是这个 -

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"));

其中currSong是用户点击的歌曲。我哪里错了。

1 个答案:

答案 0 :(得分:5)

&lt; p&gt;所以我想出代码中的问题,除了uri生成部分外,一切都是正确的,我必须添加&#34; file:///&#34;它起作用了。 所以我替换了&lt; / p&gt; &lt; pre&gt;&lt; code&gt; Uri uri = Uri.parse(song.getPath()); &LT; /代码&GT;&LT; /预&GT; &LT; p为H.以&lt; / p为H. &lt; pre&gt;&lt; code&gt; Uri uri = Uri.parse(&#34; file:///&#34; + song.getPath()); &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;它就像魅力一样! 希望其他人会觉得这很有用!&lt; / p&gt;