How to download Android files

Source: Internet
Author: User

Download files from the Internet and write them to the SD card. The idea is as follows:
1. Create an HTTP access
Httpurlconnection httpcon = (httpurlconnection) URL. openconnection ()
2. Get an input stream from httpurlconnection
Inputstream httpinputstream = httcon. getinputstream ()
3. parse the stream
Bufferedreader is generally used for obtaining.
Bufferedreader bfreader = new bufferedreader (New inputsreamreader (inputsream in ))
String line = bfreader. Readline ();
4. Create a file and generate the corresponding writer
File filewrite = new file (localpath );
Fileoutputstream fstream = new fileoutputstream (filewrite );
Bufferedwriter bfwriter = new bufferedwriter (New outputstreamwriter (fstream ));
Bfwriter. Writer (line, 0, line. Length ())
5. Remember to close the reader and writer of the stream.

If it is a non-text file, it is best to read and write it by byte.

Finally, you need to define user permissions in Android manifest. xml.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

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.