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

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);
   }



你可能感兴趣的:(android网络下载mp3文件不全问题)