Main.xml:
Copy Code code 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 Code code 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 the activity is a. */
ProgressBar PB;
TextView TV;
int fileSize;
int downloadfilesize;
String Fileex,filena,filename;
Private Handler Handler = new Handler ()
{
@Override
public void Handlemessage (msg)
{//define a handler for handling 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 Complete", 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 ("Yun_qi_img/1284565196.jpg", "/sdcard/");
Download files, parameters: first URL, second store 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 function
Filename=url.substring (Url.lastindexof ("/") + 1);
Get file name
URL myurl = new URL (URL);
URLConnection conn = Myurl.openconnection ();
Conn.connect ();
InputStream is = Conn.getinputstream ();
This.filesize = Conn.getcontentlength ()//Get file size based on response
if (this.filesize <= 0) throw new RuntimeException ("Unable to learn file size");
if (is = = null) throw new RuntimeException ("stream is null");
FileOutputStream fos = new FileOutputStream (path+filename);
Storing data in path + filename
byte buf[] = new byte[1024];
downloadfilesize = 0;
sendmsg (0);
Todo
{
Looping read
int numread = Is.read (BUF);
if (Numread = = 1)
{
Break
}
Fos.write (buf, 0, Numread);
Downloadfilesize + = Numread;
Sendmsg (1);//Update progress bar
} while (true);
Sendmsg (2);//Notify Download complete
Try
{
Is.close ();
catch (Exception ex)
{
LOG.E ("tag", "Error:" + ex.getmessage (), ex);
}
}
private void sendmsg (int flag)
{
msg = new Message ();
Msg.what = Flag;
Handler.sendmessage (msg);
}
}
After you read it, you should understand that the above is written with a loop to complete these things, byte buf[] = new byte[1024]; This sentence must be written in 1024, this can not change ah. sendmsg (0); This sentence is written in brackets of 0, this also remember, is 0 rather than 1.