Android File download progress bar implementation Code _android

Source: Internet
Author: User
Tags sendmsg
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.
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.