public class SongInfo {
private Integer id; //id,主键
private String path; //路径
private String fileName; //文件名 不包含路径
private String artist; //艺术家
public SongInfo(){
}
getXXX() setXXX()...
以扫描/mnt/sdcard上面的.mp3,.wav为例,采用 广度优先遍历
package com.mp3.scan;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import android.util.Log;
import com.mp3.entity.Lyric;
import com.mp3.entity.SongInfo;
/*音乐文件扫描*/
public class MusicScan extends Thread{
private final String root = "/mnt/sdcard/";
//private final String root_extra1 = "/mnt/sdcard/extra_sd";
private Queue<File> directoryQueue; //遍历的文件队列
private List<String> songDirList; //歌曲目录列表
private List<SongInfo> songList; //歌曲列表
private List<Lyric> lyricList; //歌词列表
private String lyricPath;
private float leastSize = 200.0f; //音乐文件最低大小 单位KB
private boolean isCompleted = false;
public MusicScan(){
directoryQueue = new LinkedList<File>();
songDirList = new ArrayList<String>();
songList = new ArrayList<SongInfo>();
lyricList = new ArrayList<Lyric>();
}
/**
* 广度优先遍历
* @param root 要遍历文件的根目录
* @return 返回搜索到的音乐文件信息列表
* @throws IOException
* */
public List scan(String root) throws IOException{
if(songList == null){
songList = new ArrayList<SongInfo>();
}else {
songList.clear();
}
if(lyricList == null){
lyricList = new ArrayList<Lyric>();
}else {
lyricList.clear();
}
//ReadMp3 read = new ReadMp3(); //解析歌曲文件对象 不需要解析TAG信息的话 就没必要这个对象
File file=new File(root); //根目录
//file = Environment.getExternalStorageDirectory();
directoryQueue.clear(); //清空队列
directoryQueue.offer(file); //入队列
while(!directoryQueue.isEmpty()){
file = directoryQueue.poll(); //出队列
boolean isSongDir = false; //标识变量 标识当前文件是否直接为含歌曲目录
File[] fileList=file.listFiles();
for (int i = 0; i < fileList.length; i++) {
//System.out.println("当前文件 "+i+" 路径:" + fileList[i].getAbsolutePath());
/* if(!fileList[i].exists()){
System.out.println(fileList[i].getName()+" 不存在!");
return null;
}*/
if(fileList[i].isFile()){
//是文件
/*if(fileList[i].length()<leastSize*1024){
//小于最低大小
continue;
}*/
if(isSong(fileList[i])){
//是歌曲文件
if(!isSongDir){
//当前状态为 非直接含歌曲目录
//避免重复添加同一目录
String songDir = new String(fileList[i].getParent());
songDirList.add(songDir);
isSongDir = true;
}
if(!fileList[i].canRead()){ //这一句很重要,不然真机上面很多文件不能访问,会出现空指针情况
//非可读文件
continue;
}
//read.setFile(fileList[i]); //设置要解析的音乐文件
//SongInfo si = read.read(); //读取TAG信息
if(si == null){
si = new SongInfo();
Log.v("MusicScan", "音乐文件TAG信息读取失败!");
}
si.setFileName(fileList[i].getName()); //文件名
si.setPath(fileList[i].getAbsolutePath()); //路径
songList.add(si); //添加进行歌曲列表songList
}else if (isLryic(fileList[i])) {
//是歌词文件
System.out.println("是歌词文件" + fileList[i].getName());
Lyric lyric = new Lyric(fileList[i].getPath(),fileList[i].getName());
lyricList.add(lyric);
}else {
//非音乐文件
//System.out.println(fileList[i].getName()+"不是音乐文件");
}
}
else //fileList[i].isDirectory() == true
{
//是目录
//System.out.println(fileList[i].getName()+"是目录");
if(!fileList[i].canRead()){
//非可读文件
continue;
}
directoryQueue.offer(fileList[i]);
}
}
}
System.out.println("scan end");
return songList;
}
这样查找到的结果(歌曲文件、歌词文件后缀)列表分别被记录在songList,lyricList列表中 !fileList[i].canRead() 这一句很重要!否则可能出现不能遍历完所有文件!