android开发,怎么遍历SD卡所有MP3文件并列举出来,能不能给我最简单的代码呀
展开全部
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//遍历媒体数据库
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//歌曲编号
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
//歌曲id
int trackId=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
//歌曲标题
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
//歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
//歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
//歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//歌曲的总播放时长:MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
//歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
//歌曲文件显示名字
String disName=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
cursor.moveToNext();
}
cursor.close();
}
//遍历媒体数据库
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//歌曲编号
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
//歌曲id
int trackId=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
//歌曲标题
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
//歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
//歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
//歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//歌曲的总播放时长:MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
//歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
//歌曲文件显示名字
String disName=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
cursor.moveToNext();
}
cursor.close();
}
更多追问追答
追问
但是我不知道怎么用啊,有具体的例子参考吗
追答
直接写在一个方法里或者运行就可以了。比如
public void ScannerMusic()
{
// 查询媒体数据库
Cursor cursor = ShakeActivity.this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
// 遍历媒体数据库
if (cursor.moveToFirst())
{
while (!cursor.isAfterLast())
{
// 歌曲编号
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
// 歌曲id
int trackId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
// 歌曲标题
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
// 歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
// 歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
// 歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
// 歌曲的总播放时长:MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
// 歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
// 歌曲文件显示名字
String disName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
Log.e("music disName=", disName);//打印出歌曲名字
cursor.moveToNext();
}
cursor.close();
}
}
在Activity的onCreate里调用ScannerMusic()方法。
展开全部
private File[] dirFiles;
private String rootPath = "/mnt/sdcard/";
private File[] mp3Files;
private int j = 0;
// 获取系统的SDCard的目录
File root = new File(rootPath);
// 如果SD卡存在的话
if (root.exists()) {
//把root下所有文件放入dirFiles中
dirFiles = root.listFiles();
}
for(int i=0;i<dirFiles.length;i++){
if(dirFiles[i].getPath().endsWith(".mp3")
|| dirFiles[i].getPath().endsWith(".MP3")){
mp3Files[j] = dirFiles[i];
j++;
}
}
mp3Files里面放的都是mp3文件 你自己填充到listview里
追问
怎么填充到listview?我刚学android开发。。。有这种遍历方式的MP3播放器源码吗?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询