从MediaStore获取音频文件路径或URI [英] Getting audio file path or URI from mediastore
问题描述
我正在制作一个将设备上所有歌曲都列出到屏幕上的应用程序,然后单击它们会打开一个共享意图,其中可以通过不同的方法(例如蓝牙,WhatsApp等)在各种设备之间共享歌曲/音频文件. 但是我无法从媒体存储中获取音频文件的文件路径或位置.
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());
}
}
我将所有歌曲存储在以下Song类的数组列表中-
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"));
currSong是用户单击的歌曲.我在哪里错了.
Where currSong is the song on which user has clicked. Where am I wrong.
推荐答案
所以我找出了代码中的问题,除uri生成部分外,其他所有内容都是正确的,我必须添加"file:///",并且该方法有效. 所以我替换了
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());
使用
Uri uri= Uri.parse("file:///"+song.getPath());
它就像魅力一样起作用! 希望其他人会发现这个有用!
and it worked like charm! Hope someone else will find this useful!
这篇关于从MediaStore获取音频文件路径或URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!