package sunquan.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import sunquan.vo.Mp3Info;
import android.os.Environment;
/**
* 需要注意这个工具类的重要性
* @author Administrator
*
*/
public class FileUtils {
public String SDCardRoot;
public FileUtils() {
super();
// TODO Auto-generated constructor stub
SDCardRoot=Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
}
/**
* 从手机sd卡上创建一个文件
* @param filename 文件名
* @param dir 目录名
* @return 文件
*/
public File createFileInSDCard(String filename,String dir){
File temp=new File(SDCardRoot+dir+File.separator+filename);
try {
// System.out.println("createFileInSDCard---"+temp);
temp.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
}
public File createSDDir(String dir){
File temp=new File(SDCardRoot+dir+File.separator);
// System.out.println(temp.mkdirs());
return temp;
}
public boolean isFileExist(String filename,String path){
File temp=new File(SDCardRoot+path+File.separator+filename);
return temp.exists();
}
public File write2SDFormInput(String path,String filename,InputStream input){
File file=null;
FileOutputStream fos=null;
createSDDir(path);
file=createFileInSDCard(filename, path);
try {
fos=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
int temp=0;
while((temp=input.read(buffer))!=-1){
fos.write(buffer, 0, temp);
}
fos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}return file;
}
public List<Mp3Info> getMp3Files(String path){
List<Mp3Info> mp3Infos=new ArrayList<Mp3Info>();
File file=new File(SDCardRoot+path);
File[] files=file.listFiles();
//在这时我们有一个问题也就是在查找我们目录下的文件时,无法把对应的mp3文件来lrc文件
//对应起来。
for(int i=0;i<files.length;i++){
//1.遍历你得到的文件
//2.在第一次当list为空的时候特别设置
//3.在第二次的时候,就进入后面的代码了
// 3.1如果说得到的是一个mp3文件,那我们就要找list中有没有已经存在的krc对象
// 如果有了,那就直接找到这个对象,把相应的 mp3属性设置一下就可以了
// 如果没有,就要创建一个对象了,然后设置相应的mp3属性。
if(mp3Infos.size()==0){
if(files[i].getName().endsWith("mp3")){
//如果说在里面找到了相对应的mp3文件,也就是说,我们已经在上面声明过
//对象,那么我们需要把这个对象找到,然后把相应的对应的一些参数设置起来
Mp3Info mp3Info=new Mp3Info();
mp3Info.setMp3Name(files[i].getName());
mp3Info.setMp3Size(files[i].length()+"");
mp3Infos.add(mp3Info);
System.out.println("first got file is mp3..........");
continue;
}else if(files[i].getName().endsWith("krc"))
{
Mp3Info mp3Info=new Mp3Info();
mp3Info.setLrcName(files[i].getName());
mp3Info.setLrcSize(files[i].length()+"");
mp3Infos.add(mp3Info);
System.out.println("first got file is krc..........");
continue;
}
}
if(files[i].getName().endsWith("mp3")){
//如果我们找到一个文件是歌词文件时,我们首先
//1.在list中查找一下有没有对应的krc文件了
System.out.println("second........");
String filename=files[i].getName();
int dot=filename.indexOf(".");
String filenameUp=filename.substring(0, dot);
boolean b=false;
//这里有一个问题,就是在想在实现mp3文件和krc文件同时下载的时候,一开始的
//时候这时的原来的list是空的!我们根本一开始就进入不到这个list里,所以我们要在
//一开始的加进去,确保不是空的
for(int j=0;j<mp3Infos.size();j++){
System.out.println("in");
System.out.println("mp3Infos size---"+mp3Infos.size());
System.out.println("mp3Infos.get(j).getMp3Name()---"+mp3Infos.get(j).getLrcName());
if(mp3Infos.get(j).getLrcName()!=null&&mp3Infos.get(j).getLrcName().equals(filenameUp+".krc")){
//如果说在里面找到了相对应的mp3文件,也就是说,我们已经在上面声明过
//对象,那么我们需要把这个对象找到,然后把相应的对应的一些参数设置起来
mp3Infos.get(j).setMp3Name(filenameUp+".mp3");
mp3Infos.get(j).setMp3Size(files[i].length()+"");
b=true;
break;
}
}
if(!b){
//下面是没有在List中找到这个对象,那么我们就来创建这个对象,并设置
//相应的参数值
Mp3Info mp3Info=new Mp3Info();
mp3Info.setMp3Name(files[i].getName());
mp3Info.setMp3Size(files[i].length()+"");
mp3Infos.add(mp3Info);
}
}
// Mp3Info mp3Info=new Mp3Info();
// mp3Info.setMp3Name(files[i].getName());
// mp3Info.setMp3Size(files[i].length()+"");
// mp3Infos.add(mp3Info);
else if(files[i].getName().endsWith("krc"))
{
//如果我们找到一个文件是歌词文件时,我们首先
//1.在list中查找一下有没有对应的mp3文件了
String filename=files[i].getName();
int dot=filename.indexOf(".");
String filenameUp=filename.substring(0, dot);
System.out.println("fileName..."+filenameUp);
boolean b=false;
for(int j=0;j<mp3Infos.size();j++){
System.out.println("mp3Infos.get(j).getMp3Name()"+mp3Infos.get(j).getMp3Name());
if(mp3Infos.get(j).getMp3Name()!=null&&mp3Infos.get(j).getMp3Name().equals(filenameUp+".mp3")){
//如果说在里面找到了相对应的mp3文件,也就是说,我们已经在上面声明过
//对象,那么我们需要把这个对象找到,然后把相应的对应的一些参数设置起来
mp3Infos.get(j).setLrcName(filenameUp+".krc");
mp3Infos.get(j).setLrcSize(files[i].length()+"");
b=true;
System.out.println("thrid........");
break;
}
}
if(!b){
//下面是没有在List中找到这个对象,那么我们就来创建这个对象,并设置
//相应的参数值
Mp3Info mp3Info=new Mp3Info();
mp3Info.setLrcName(files[i].getName());
mp3Info.setLrcSize(files[i].length()+"");
mp3Infos.add(mp3Info);
}
}
}
return mp3Infos;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mp3player.rar (58个子文件)
mp3player
.project 845B
default.properties 362B
bin
mp3player.apk 30KB
sunquan
utils
AppConstance.class 192B
FileUtils.class 6KB
AppConstance$MSG.class 484B
xml
Mp3ListContentHandler.class 3KB
vo
Mp3Info.class 2KB
mp3player
R$string.class 603B
LocalListActivity.class 3KB
Mp3ListActivity.class 5KB
R$layout.class 524B
MainActivity.class 2KB
R$drawable.class 483B
PlayerActivity.class 3KB
R.class 524B
R$attr.class 337B
service
PlayerService.class 3KB
DownLoadService.class 1KB
DownLoadService$DownLoadThread.class 2KB
DownLoadService$DownLoadKrcThread.class 2KB
R$id.class 579B
download
HttpDownload.class 3KB
resources.ap_ 17KB
classes.dex 22KB
proguard.cfg 1KB
AndroidManifest.xml 1KB
src
sunquan
utils
AppConstance.java 204B
FileUtils.java 6KB
xml
Mp3ListContentHandler.java 2KB
vo
Mp3Info.java 1KB
mp3player
PlayerActivity.java 4KB
LocalListActivity.java 3KB
MainActivity.java 1KB
service
PlayerService.java 2KB
DownLoadService.java 3KB
Mp3ListActivity.java 6KB
download
HttpDownload.java 3KB
res
values
strings.xml 420B
layout
player.xml 821B
listresource.xml 876B
main.xml 704B
locallist.xml 559B
remotelist.xml 559B
drawable-ldpi
pause.gif 220B
start.gif 227B
stop.gif 221B
icon.png 2KB
drawable-hdpi
pause.gif 220B
start.gif 227B
stop.gif 221B
icon.png 4KB
drawable-mdpi
pause.gif 220B
start.gif 227B
stop.gif 221B
icon.png 3KB
.classpath 280B
gen
sunquan
mp3player
R.java 2KB
assets
共 58 条
- 1
资源评论
- sxcwyc12012-06-10搁我电脑里打不开,也不知道电脑是哪出问题了
- cshcool2012-09-05我也无法正常运行
加权不平权
- 粉丝: 2391
- 资源: 54
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功