[Android instance] File Download and storage of downloaded files to the SD card

Source: Internet
Author: User
Public  String downloadtext (string urlstr) {stringbuffer sb = New  Stringbuffer (); string line = Null  ; Bufferedreader Buffer = Null  ;  Try  {  //  Create a URL object Url = New  URL (urlstr );  // Create an HTTP Connection  Try  {Httpurlconnection urlconn = (Httpurlconnection) URL. openconnection ();  //  Read data using Io streams Buffer = New Bufferedreader ( New  Inputstreamreader (urlconn. getinputstream ()));  While (Line = buffer. Readline ())! = Null  ) {Sb. append (line );}}  Catch (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}}  Catch  (Malformedurlexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Return  SB. tostring ();} 
 /*  * This function returns an integer-1, indicating an error occurred while downloading the object. * 0: indicates that the downloaded file is successful. * 1: indicates that the downloaded file already exists.  */ Public   Int  Downloadfile (string urlstr, string path, string filename) {inputstream = Null  ; Gotosdcard = New  Gotosdcard ();  If (Gotosdcard. isfileexist (path + Filename )){  Return 1 ;}  Else  {  Try {URL = New  URL (urlstr); httpurlconnection urlconn = (Httpurlconnection) URL. openconnection (); inputstream = Urlconn. getinputstream (); file resultfile = Gotosdcard. write2sdfrominput (path, filename, inputstream ); //  Save the data stream to the SD card  If (Resultfile = Null  ){  Return -1 ;}} Catch  (Malformedurlexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Finally  {  Try  {Inputstream. Close ();}  Catch (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}}}  Return 0 ;} 
 Public   Class  Gotosdcard {  Private String sdpath = Null  ;  Public  String getsdpath (){  Return Sdpath ;}  Public  Gotosdcard (){  //  Obtain the directory of the current external storage device.  //  Sdcard Sdpath = environment. getexternalstoragedirectory () + "" ; System. Out. println ( "Sdpath =" + Sdpath );}  /*  * Create a file on the SD card  */  Public  File creatsdfile (string filenmae) {file File =New File (sdpath + Filenmae );  Try  {File. createnewfile ();}  Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Return  File ;}  /*  * Create a directory on the SD card  */  Public File creatsddir (string dirname) {file dir = New File (sdpath + Dirname );  If (! Dir. exists () {dir. mkdirs ();}  Return  Dir ;}  /*  * Determine whether the folder on the SD card exists  */  Public   Boolean  Isfileexist (string filename) {file File = New File (sdpath +Filename );  Return  File. exists ();}  /*  * Write data from an inputsteam instance to the SD card.  */  Public  File write2sdfrominput (string path, string filename, inputstream input) {system. Out. println ( "Path =" + path + "; filename =" + filename + ";" ); File File = Null  ; File folder = Null  ; Outputstream output =Null  ;  Try  {Folder = Creatsddir (PATH); system. Out. println ( "Folder =" + Folder); File = Creatsdfile (path + Filename); system. Out. println ( "File =" + File); Output = New  Fileoutputstream (File );  Byte Buffer [] = New   Byte [4*1024];  While (Input. Read ())! =-1 ) {Output. Write (buffer);} output. Flush ();}  Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Finally  {  Try  {Output. Close ();}  Catch  (Exception e) {e. printstacktrace ();}}  Return File ;}} 
 
<Uses-PermissionAndroid: Name= "Android. Permission. Internet"/>// Permissions required for downloading files from the network<Uses-PermissionAndroid: Name= "Android. Permission. write_external_storage"/>// Write permission to the SD card

 

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.