使用MeidaStore.Audio获得手机中的音频文件
MediaStore是安卓系统自带的多媒体系统数据库,他在每次开机时刷新一次,可以通过Cursor这个类对数据库进行访问与修改,修改之后需用广播强制刷新。
使用Cursor必须通过Context获得ContentResolver的对象实例。
Cursor cursor1 = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
View Code
代码中的context既是调用该函数的活动的对象,先上个封装了音乐信息的类
public class Music { private String filename; private String title; private int during; private String singer; private String type; private String size; private String fileUrl; public Music(String filename,String title,int during,String singer,String type,String size,String fileUrl){ this.filename = filename; this.title = title; this.during = during; this.singer = singer; this.type = type; this.size = size; this.fileUrl = fileUrl; } public String toString(){ return "song[filename="+filename+", title="+title+", during="+during+", singer="+singer+", type="+type+", size="+size+", fileUrl="+fileUrl+"]"; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getDuring() { return during; } public void setDuring(int during) { this.during = during; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getFileUrl() { return fileUrl; } public void setFileUrl(String fileUrl) { this.fileUrl = fileUrl; } }
下面就是用Cursor操作数据库的帮助类了
public class MusicHelper{ public static List<Music> songs = new ArrayList<Music>(); public static List<Music> getAllsongs(Context context){ Cursor cursor1 = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null); getcorrSongs(cursor1); Cursor cursor2 = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,null,null,null,null); getcorrSongs(cursor2); return songs; } public static void getcorrSongs(Cursor cursor){ Music song = null; if(cursor != null){ while(cursor.moveToNext()){ String filename = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); int during = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); String singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); String type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE)); String size = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); String date = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song = new Music(filename,title,during,singer,type,size,date); songs.add(song); } cursor.close(); } } }
参数content即用活动的this引用传入。
allsongs = MusicHelper.getAllsongs(this);
版权声明:本文为liboyan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。