android下载单个文件时时更新进度条

package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DownFileActivity extends Activity {
 int fileSize;
 int downFileSize;
 Button m_Button;
 TextView m_TextView;
 ProgressBar m_ProgressBar;
 String fileName;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        m_Button = (Button) findViewById(R.id.button01);
        m_TextView = (TextView) findViewById(R.id.textView01);
        m_ProgressBar = (ProgressBar) findViewById(R.id.progressBar01);
        m_ProgressBar.setVisibility(View.GONE);
        m_Button.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   m_ProgressBar.setVisibility(View.VISIBLE);
   
   new Thread(){
    public void run(){
     try{
      String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
      downLoadFile("http://hiphotos.baidu.com/846322057/pic/item/81547a892004af28c8fc7af1.jpg",path);
     } catch (ClientProtocolException e) { 
                     // TODO Auto-generated catch block 
                     e.printStackTrace(); 
                 } catch (IOException e) { 
                     // TODO Auto-generated catch block 
                     e.printStackTrace(); 
                 } 
             } 
         }.start();
   }
         
        });
       
    }
   
   private Handler handler = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   //定义一个Handler用来处理线程与UI间的通讯
   if(!Thread.currentThread().isInterrupted()){
    switch(msg.what){
    case 0:
     m_ProgressBar.setMax(fileSize);
    case 1:
     m_ProgressBar.setProgress(downFileSize);
     int count = downFileSize * 100 / fileSize;
     m_TextView.setText("当前下载进度为" + count + "%");
     break;
    case 2:
     m_TextView.setText("文件下载完成");
     m_ProgressBar.setVisibility(View.GONE);
     Toast.makeText(DownFileActivity.this, "文件下载完成", 1).show();
     break;
    case -1:
     String error = msg.getData().getString("error");
     Toast.makeText(DownFileActivity.this, error, 1).show();
     break;
    }
   }
   super.handleMessage(msg);
  }
     
    };
    public void sendMsg(int flag){
     Message msg = new Message();
     msg.what = flag;
     handler.sendMessage(msg);
    }
   
    public void downLoadFile(String url, String path) throws IOException{
     fileName = url.substring(url.lastIndexOf("/") + 1);
     //path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
     
     URL strUrl = new URL(url);
     HttpURLConnection conn = (HttpURLConnection) strUrl.openConnection();
     
     InputStream input = conn.getInputStream();
     //获取文件的大小
     this.fileSize = conn.getContentLength();
     
     if(this.fileSize <= 0) throw new RuntimeException("获取文件出错");
     if(input == null) throw new RuntimeException("stream is null");
     
     FileOutputStream out = new FileOutputStream(path+fileName);
     byte[] buffer = new byte[1024];
     System.out.println(path+"path........");
     downFileSize = 0;
     sendMsg(0);
     do{
      //循环读取数据
      int readNum = input.read(buffer);
      if(readNum == -1){
       break;
      }
      out.write(buffer, 0, readNum);
      downFileSize += readNum;
      //更新进度条进度
      sendMsg(1);
     }while(true);
     //文件下载完成
     sendMsg(2);
     try{
      input.close();
     }catch(Exception e){
      Log.e("tag", "error是:" + e.getMessage(), e);
     }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值