java cursor获取mp3,从MediaStore获取音频文件路径或URI

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 CREATOR = new Parcelable.Creator() {

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.

解决方案

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值