The download size of the android file is inconsistent with that of the network (too large)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.