Android中下载进度条格式,Android开发如何实现文件下载进度条

释放双眼,带上耳机,听听看~!

今天和小编狗在这里学习下载进度的实现吧,这段代码是网上找的,自己做了些小改,通过模拟器测试。文件下载进度条控制(就是为了高清壁纸加个进度条),自己研究了好久,但是进度条只能显示缓存写入文件的进度,不能显示下载进度。找了好久,终于找到一段用的代码,所以记录下来,大家分享下Android文件下载进度条的实现代码。

main.xml:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=""

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="100"

mce_style="?android:attr/progressBarStyleHorizontal"

/>

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值