根据音乐文件和LRC文件的绝对路径下载到sdcard指定目录中

/**
* 下载音乐文件到指定目录
* @param musicPath
* @param musicName
* @param singerName
*/
public void downloadMusic(final String musicPath, String musicName,
String singerName ) {

String dir = "ccpod/downloadMusic";
final String filePath = "/sdcard/ccpod/downloadMusic/" +
musicName +"-"+ singerName +".mp3";
FileService fileService = new FileService();
fileService.createFileDir(dir);
fileService.createFile(filePath);

Runnable r = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
int count;
try {
URL url = new URL(musicPath);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(filePath);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
System.out.println(count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("error",e.getMessage().toString());
System.out.println(e.getMessage().toString());
}
}
};
new Thread(r).start();
}


/**
* 下载歌词文件到指定目录
* @param lrcPath
* @param musicName
* @param singerName
*/
public void downloadLyric(final String lrcPath, String musicName,
String singerName) {


String dir = "ccpod/lyric";
final String filePath = "/sdcard/ccpod/lyric/" + musicName + "-"
+ singerName + ".lrc";
FileService fileService = new FileService();
fileService.createFileDir(dir);
fileService.createFile(filePath);

Runnable r = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(lrcPath);
InputStream input = new BufferedInputStream(
url.openStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(input,"GB2312"));

Writer writer = new OutputStreamWriter(new FileOutputStream(filePath));
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data+ "\r\n");
}
writer.write(sb.toString());
writer.close();
input.close();
} catch (Exception e) {
Log.e("error", e.getMessage().toString());
}
}
};
new Thread(r).start();
}


[color=red]下载歌词时要注意inputStream的转码,由于服务器中的xml文件是用gb2312编码,所以应按照原来的格式读进来[/color]


其中创建文件和文件目录代码如下:
FileService.java
public boolean createFileDir(String dirName) {
dirName = Environment.getExternalStorageDirectory() + "/" + dirName;
File dir = new File(dirName);
if (dir.exists()) {
return false;
}
if (!dirName.endsWith("/")) {
dirName += File.separator;
}
if (dir.mkdirs()) {
return true;
} else
return false;
}

public boolean createFile(String fileName){
File file = new File(fileName);
if(file.exists()){
System.out.println("创建单个文件:"+ fileName + "失败,目标文件已存在");
return false;
}
if(!file.getParentFile().exists()){
System.out.println("目标文件所在目录不存在,准备创建它");
return false;
}
try {
if(file.createNewFile()){
return true;
}else{
return false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}



[img]
[img]http://dl.iteye.com/upload/attachment/593798/1b614649-758c-3604-bb5b-4156d479e0af.png[/img]
[/img]

[color=brown]参考资料:
Android中文乱码彻底解决
http://www.iteye.com/topic/509046


Android 中下载文件到sdcard和进度条小结
http://www.linuxidc.com/Linux/2011-08/40941.htm


Android中的自动下载歌词
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=61595[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值