您的位置:首页 > 理论基础 > 计算机网络

android网络下载mp3文件不全问题

2016-03-02 18:24 330 查看
public File writeToSDPATHFromInput(String fileName,InputStream inputstream)
{
File file=null;
OutputStream outputstream=null;
try
{
file=createFile(MyResources.getUnitMp3Path(),fileName);
outputstream=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
int length = 0;
//将输入流中的内容先输入到buffer中缓存,然后用输出流写到文件中
while((length = inputstream.read(buffer)) != -1)
{
outputstream.write(buffer,0,length);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try {
outputstream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}


看fileutil包下写入文件的部分,问题出错的地方在这里,因为不一定每次都是读取4*1024的字节数据

byte[] buffer = new byte[4*1024];
while((input.read(buffer)) != -1){
output.write(buffer);
}
改成这样:增加偏移量,根据每次Inputstream读入的数据的大小,再写入OutputStream中。
byte[] buffer = new byte[4*1024];
int length = 0;
while((length = input.read(buffer)) != -1){
output.write(buffer,0,length);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: