<?xml version="1.0" encoding="utf-8"?>         

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);      }            }  


更多相关文章

  1. 2013.12.23 ——— android assents文件路径
  2. Android定义的路径全局变量
  3. Android Q中外部存储盘路径正则表达式的理解
  4. android内部外部存储卡路径的获取
  5. 设置Android SDK tools工作路径环境变量
  6. Android 模拟器中AVD路径的修改(WIN7)
  7. katalon设置Android SDK路径

随机推荐

  1. flex容器与项目的常用属性
  2. artipub 发布测试
  3. 百度总监:测试稳定性三板斧,你了解多少?
  4. 2021.2.23
  5. 我的第24个代码
  6. 我的第23个代码
  7. 工作十余年,还是一直被问 委托和事件 有什
  8. 配置文件中的数据库连接串加密了,你以为我
  9. Enumerable 下又有新的扩展方法啦,快来一
  10. 遍历 Dictionary,你会几种方式?