Download Android files

Source: Internet
Author: User
Public void downfile (string URL, string path, string filename) throws ioexception {If (filename = NULL | filename = "") This. filename = URL. substring (URL. lastindexof ("/") + 1); elsethis. filename = filename; // get the file name. If you enter a new file name, use the new file name url = new URL (URL); urlconnection conn = URL. openconnection (); Conn. connect (); inputstream is = Conn. getinputstream (); this. filesize = Conn. getcontentlength (); // get text based on the response Part Size if (this. filesize <= 0) {// The length of the retrieved content is 0 throw new runtimeexception ("unable to know the file size");} If (is = NULL) {// The stream sendmsg (down_error) is not downloaded; throw new runtimeexception ("file cannot be obtained");} fileoutputstream Fos = new fileoutputstream (path + this. filename); // create a memory stream for writing to the file, which flows to the target Write File byte Buf [] = new byte [1024]; downloadfileposition = 0; int numread; while (numread = is. read (BUF ))! =-1) {FOS. write (BUF, 0, numread); downloadfileposition + = numread} Try {is. close () ;}catch (exception ex) {;}} use this Code You can save the content to the SD card and other devices. To use the network, you must have access to the network. You need to add this item by yourself. Do not add it here! The above Code does not implement the progress bar function. If you want to implement the progress bar function, I now consider using messages for sending prompts, first implement a message. Code private handler downloadhandler = new handler () {// used to receive messages, processing progress bar @ overridepublic void handlemessage (Message MSG) {// received message, and processes the received message. If (! Thread. currentthread (). isinterrupted () {Switch (MSG. what) {Case down_start: Pb. setmax (filesize); // set the start length case down_position: Pb. setprogress (downloadfileposition); // sets the progress break; Case down_complete: Toast. maketext (downloadfiletest. this, "Download complete! ", 1). Show (); // The Break prompt is displayed. Case down_error: string error = msg. getdata (). getstring (" Download error! "); Toast. maketext (downloadfiletest. this, error, 1 ). show (); break;} super. handlemessage (MSG) ;}}; in this way, you only need to send the corresponding message during the download, and the corresponding prompt will be displayed! I hope it will be helpful for your thinking! Only one idea is provided here. If you have better ideas, please contact us!
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.