android从手机数据库中取音乐数据
直接贴代码
public void getMp3(){ list = new ArrayList<>(); Cursor mAudioCursor = this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,// 字段 没有字段 就是查询全部信息 相当于SQL语句中的 “ * ” null, // 查询条件 null, // 条件的相应?的參数 MediaStore.Audio.AudioColumns.TITLE);// 排序方式 // 循环输出歌曲的信息 for (int i = 0; i < mAudioCursor.getCount(); i++) { mAudioCursor.moveToNext(); // 找到歌曲标题和总时间相应的列索引 long id = mAudioCursor.getLong(mAudioCursor.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id String title = mAudioCursor.getString((mAudioCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));// 音乐标题 String artist = mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));// 艺术家 long duration = mAudioCursor.getLong(mAudioCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));// 时长 long size = mAudioCursor.getLong(mAudioCursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小 String url = mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径 int isMusic = mAudioCursor.getInt(mAudioCursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));// 是否为音乐 if(isMusic!=0){//仅仅有当是音乐的时候才保存 Mp3Info mp3Info=new Mp3Info(id,title,artist,duration,size,url); list.add(mp3Info); } } }
package flyfish.com.mp3play; /** * Created by Administrator on 2015/8/18 0018. */ public class Mp3Info { private long id; private String title; private String artist; private long duration; private long size; private String url; public Mp3Info( long id, String title,String artist, long duration, long size, String url) { this.artist = artist; this.duration = duration; this.id = id; this.size = size; this.title = title; this.url = url; } public Mp3Info() { } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } public long getId() { return id; } public void setId(long id) { this.id = id; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Override public String toString() { return "Mp3Info{" + "artist='" + artist + ''' + ", id=" + id + ", title='" + title + ''' + ", duration=" + duration + ", size=" + size + ", url='" + url + ''' + '}'; } }