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

Getting audio file path or URI from mediastore

我正在制作一个应用程序,它将设备上的所有歌曲列在屏幕上,点击它们会打开一个共享意图,歌曲/音频文件可以通过不同的方法(如蓝牙、WhatsApp 等)在各种设备之间共享。 但是我无法从媒体存储中获取音频文件的文件路径或位置。

这就是我获取歌曲的方式-

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

    }
}

我将所有歌曲存储在下面的 class 歌曲的数组列表中-

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 是用户点击的歌曲。我哪里错了。

所以我找出了代码中的问题,除了 uri 生成部分外,一切都是正确的,我必须添加 "file:///" 并且它起作用了。 所以我替换了

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

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

而且效果很好! 希望其他人会觉得这很有用!