Today, I am writing a program for downloading files and searching for a copy on the Internet to download MP3 files. However, it is found that the downloaded MP3 file is larger than the original file, and there will be noises in the MP3 file. After adding the Log to the Log, we found that the length of the stream obtained from the network stream is about 3000000, but the length of the file obtained from the SD card is about 4000000. Finally, we found that the download method is incorrect: you only need to correct a function: Download the main code of the original file:
OutputStream output=new FileOutputStream(file); byte[] buffer=new byte[1024*4]; while (istream.read(buffer)!=-1) { output.write(buffer); } output.flush(); output.close(); istream.close();
Primary code after correction:
OutputStream OS = new FileOutputStream (file); byte [] buffer = new byte [1024*4]; int len =-1; while (len = is. read (buffer ))! =-1) {OS. write (buffer, 0, len); // use another overload here to prevent stream writing problems .} OS. flush (); OS. close (); is. close ();
The above are the problems encountered during code writing today, and the correct party.