Main. xml:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView android: id = "@ + id/TV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""
/>
<ProgressBar android: id = "@ + id/down_pb"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: max = "100"
Style = "? Android: attr/progressBarStyleHorizontal "mce_style = "? Android: attr/progressBarStyleHorizontal"
/>
</LinearLayout>
Main. java:Copy codeThe Code is as follows: package com. pocketdigi. download;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java.net. URL;
Import java.net. URLConnection;
Import org. apache. http. client. ClientProtocolException;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. util. Log;
Import android. widget. ProgressBar;
Import android. widget. TextView;
Import android. widget. Toast;
Public class main extends Activity {
/** Called when the activity is first created .*/
Pb ssbar pb;
TextView TV;
Int fileSize;
Int downLoadFileSize;
String fileEx, fileNa, filename;
Private Handler handler = new Handler ()
{
@ Override
Public void handleMessage (Message msg)
{// Define a Handler for processing the communication between the download thread and the UI
If (! Thread. currentThread (). isInterrupted ())
{
Switch (msg. what)
{
Case 0:
Pb. setMax (fileSize );
Case 1:
Pb. setProgress (downLoadFileSize );
Int result = downLoadFileSize * 100/fileSize;
TV. setText (result + "% ");
Break;
Case 2:
Toast. makeText (main. this, "file download completed", 1). show ();
Break;
Case-1:
String error = msg. getData (). getString ("error ");
Toast. makeText (main. this, error, 1). show ();
Break;
}
}
Super. handleMessage (msg );
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Pb = (ProgressBar) findViewById (R. id. down_pb );
TV = (TextView) findViewById (R. id. TV );
New Thread (){
Public void run (){
Try {
Down_file ("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg", "/sdcard /");
// Download the file. Parameter: First URL and second storage path
} Catch (ClientProtocolException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}. Start ();
}
Public void down_file (String url, String path) throws IOException {
// Download the Function
Filename = url. substring (url. lastIndexOf ("/") + 1 );
// Get the file name
URL myURL = new URL (url );
URLConnection conn = myURL. openConnection ();
Conn. connect ();
InputStream is = conn. getInputStream ();
This. fileSize = conn. getContentLength (); // obtain the file size according to the response
If (this. fileSize <= 0) throw new RuntimeException ("unable to know the file size ");
If (is = null) throw new RuntimeException ("stream is null ");
FileOutputStream fos = new FileOutputStream (path + filename );
// Save the data to the path + file name
Byte buf [] = new byte [1024];
DownLoadFileSize = 0;
SendMsg (0 );
Do
{
// Read cyclically
Int numread = is. read (buf );
If (numread =-1)
{
Break;
}
Fos. write (buf, 0, numread );
DownLoadFileSize + = numread;
SendMsg (1); // update the progress bar
} While (true );
SendMsg (2); // notification that the download is complete
Try
{
Is. close ();
} Catch (Exception ex)
{
Log. e ("tag", "error:" + ex. getMessage (), ex );
}
}
Private void sendMsg (int flag)
{
Message msg = new Message ();
Msg. what = flag;
Handler. sendMessage (msg );
}
}
After reading this, we should understand that we have written these things in a loop. byte buf [] = new byte [1024]; in this sentence, everyone must write 1024. This cannot be changed. SendMsg (0); the brackets in this sentence are 0. Remember that it is 0 rather than 1.