Asynctask is a good thing. You can use it for the download progress bar.CodeThe description is as follows:
Package com. example. downloadfile;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java.net. httpurlconnection;
Import java.net. url;
Import Android. App. activity;
Import Android. App. Dialog;
Import Android. App. progressdialog;
Import Android. OS. asynctask;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. util. log;
Import Android. widget. textview;
Public class downloadfile extends activity {
Public static final string log_tag = "test ";
Private progressdialog mprogressdialog;
Public static final int dialog_download_progress = 0;
File rootdir = environment. getexternalstoragedirectory ();
// Define the file name to download
Public String filename = "test.jpg ";
Public String fileurl = "https://lh4.googleusercontent.com/-HiJOyupc-tQ/TgnDx1_HDzI/AAAAAAAAAWo/DEeOtnRimak/s800/DSC04158.JPG ";
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Textview TV = new textview (this );
TV. settext ("android download file with progress bar ");
// Check whether the download directory exists
Checkandcreatedirectory ("/mydownloads"); autumn story
// Execute asynctask
New downloadfileasync(cmd.exe cute (fileurl );
}
Class downloadfileasync extends asynctask <string, String, string> {
@ Override
Protected void onpreexecute (){
Super. onpreexecute ();
Showdialog (dialog_download_progress );
}
@ Override
Protected string doinbackground (string... aurl ){
Try {
// Connection address
URL u = new URL (fileurl );
Httpurlconnection c = (httpurlconnection) U. openconnection ();
C. setrequestmethod ("get ");
C. setdooutput (true );
C. Connect ();
// Calculate the file length
Int lenghtoffile = C. getcontentlength ();
Fileoutputstream F = new fileoutputstream (new file (rootdir + "/my_downloads/", filename ));
Inputstream in = C. getinputstream ();
// Download code
Byte [] buffer = new byte [1024];
Int len1 = 0;
Long Total = 0;
While (len1 = in. Read (buffer)> 0 ){
Total + = len1; // Total = total + len1
Publishprogress ("" + (INT) (total * 100)/lenghtoffile ));
F. Write (buffer, 0, len1 );
}
F. Close ();
} Catch (exception e ){
Log. D (log_tag, E. getmessage ());
}
Return NULL;
}
Protected void onprogressupdate (string... progress ){
Log. D (log_tag, progress [0]);
Mprogressdialog. setprogress (integer. parseint (Progress [0]);
}
@ Override
Protected void onpostexecute (string unused ){
// Dismiss the dialog after the file was downloaded
Dismissdialog (dialog_download_progress );
}
}
Public void checkandcreatedirectory (string dirname ){
File new_dir = new file (rootdir + dirname );
If (! New_dir.exists ()){
New_dir.mkdirs ();
}
}
@ Override
Protected dialog oncreatedialog (int id ){
Switch (ID ){
Case dialog_download_progress: // we set this to 0
Mprogressdialog = new progressdialog (this );
Mprogressdialog. setmessage ("downloading file ...");
Mprogressdialog. setindeterminate (false );
Mprogressdialog. setmax (100 );
Mprogressdialog. setprogressstyle (progressdialog. style_horizontal );
Mprogressdialog. setcancelable (true );
Mprogressdialog. Show ();
Return mprogressdialog;
Default:
Return NULL;
}
}
}