Android文件下载进度条的实现代码
程序员文章站
2023-12-01 08:34:04
main.xml:复制代码 代码如下:
main.xml:
<?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:
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. */
progressbar pb;
textview tv;
int filesize;
int downloadfilesize;
string fileex,filena,filename;
private handler handler = new handler()
{
@override
public void handlemessage(message msg)
{//定义一个handler,用于处理下载线程与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, "文件下载完成", 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/");
//下载文件,参数:第一个url,第二个存放路径
} 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{
//下载函数
filename=url.substring(url.lastindexof("/") + 1);
//获取文件名
url myurl = new url(url);
urlconnection conn = myurl.openconnection();
conn.connect();
inputstream is = conn.getinputstream();
this.filesize = conn.getcontentlength();//根据响应获取文件大小
if (this.filesize <= 0) throw new runtimeexception("无法获知文件大小 ");
if (is == null) throw new runtimeexception("stream is null");
fileoutputstream fos = new fileoutputstream(path+filename);
//把数据存入路径+文件名
byte buf[] = new byte[1024];
downloadfilesize = 0;
sendmsg(0);
do
{
//循环读取
int numread = is.read(buf);
if (numread == -1)
{
break;
}
fos.write(buf, 0, numread);
downloadfilesize += numread;
sendmsg(1);//更新进度条
} while (true);
sendmsg(2);//通知下载完成
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);
}
}
大家看了以后就应该明白了,上面写的是用一个循环来完成的这些事情,byte buf[] = new byte[1024];这句话中大家一定要写1024,这个可不能改变呀。sendmsg(0);这句的括号里写的是0,这个也要记得,是0而不是1.
复制代码 代码如下:
<?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:
复制代码 代码如下:
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. */
progressbar pb;
textview tv;
int filesize;
int downloadfilesize;
string fileex,filena,filename;
private handler handler = new handler()
{
@override
public void handlemessage(message msg)
{//定义一个handler,用于处理下载线程与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, "文件下载完成", 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/");
//下载文件,参数:第一个url,第二个存放路径
} 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{
//下载函数
filename=url.substring(url.lastindexof("/") + 1);
//获取文件名
url myurl = new url(url);
urlconnection conn = myurl.openconnection();
conn.connect();
inputstream is = conn.getinputstream();
this.filesize = conn.getcontentlength();//根据响应获取文件大小
if (this.filesize <= 0) throw new runtimeexception("无法获知文件大小 ");
if (is == null) throw new runtimeexception("stream is null");
fileoutputstream fos = new fileoutputstream(path+filename);
//把数据存入路径+文件名
byte buf[] = new byte[1024];
downloadfilesize = 0;
sendmsg(0);
do
{
//循环读取
int numread = is.read(buf);
if (numread == -1)
{
break;
}
fos.write(buf, 0, numread);
downloadfilesize += numread;
sendmsg(1);//更新进度条
} while (true);
sendmsg(2);//通知下载完成
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);
}
}
大家看了以后就应该明白了,上面写的是用一个循环来完成的这些事情,byte buf[] = new byte[1024];这句话中大家一定要写1024,这个可不能改变呀。sendmsg(0);这句的括号里写的是0,这个也要记得,是0而不是1.