Chinaunix首页 | 论坛 | 认证专区 | 博客 登录 | 注册

醉清风wylong.blog.chinaunix.net

梅花疏淡、清风笑烟雨

  • 博客访问: 688748
  • 博文数量: 174
  • 博客积分: 1784
  • 博客等级: 上尉
  • 技术积分: 2137
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-05 22:20
  • 认证徽章:
个人简介

发上等愿,结中等缘,享下等福;择高处立,就平处坐,向宽处行。

文章分类

全部博文(174)

文章存档

2017年(4)

2016年(13)

2015年(22)

2014年(21)

2013年(29)

2012年(36)

2011年(49)

微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

文件上传下载进度条 2012-04-19 10:51:25

分类: 嵌入式


点击(此处)折叠或打开

  1. 进度条是一个很头疼的问题,当然弄明白了也很好理解,用起来也很方便,大都会涉及到两个,一个是圆形,另外一个则是长方形,一点一点推进的

  2. 对于圆形比较简单,先说说长方形的原理:

  3. 首先需要获取文件大小,在循环写的时候就进行加减乘法运算,哈哈,就这么简单!

  4. 给个例子吧:
  5.  public class Android_X5_SOSO_9_9_2Activity extends Activity {
  6.     /** Called when the activity is first created. */
  7.     ProgressBar pb;
  8.     TextView tv;
  9.     int fileSize;
  10.     int downLoadFileSize;
  11.     String fileEx,fileNa,filename;
  12.     private Handler handler = new Handler()
  13.      {
  14.      @Override
  15.      public void handleMessage(Message msg)
  16.      {//定义一个Handler,用于处理下载线程与UI间通讯

  17.      if (!Thread.currentThread().isInterrupted())
  18.      {
  19.      switch (msg.what)
  20.      {
  21.      case 0:
  22.      pb.setMax(fileSize);
  23.      case 1:
  24.      pb.setProgress(downLoadFileSize);
  25.      int result = downLoadFileSize * 100 / fileSize;
  26.      tv.setText(result + "%");
  27.      break;
  28.      case 2:
  29.      Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, "文件下载完成", 1000).show();
  30.      break;

  31.      case -1:
  32.      String error = msg.getData().getString("error");
  33.      Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, error, 1000).show();
  34.      break;
  35.      }
  36.      }
  37.      super.handleMessage(msg);
  38.      }
  39.      };
  40.     @Override
  41.     public void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.main);
  44.         
  45.         pb=(ProgressBar)findViewById(R.id.down_pb);
  46.         tv=(TextView)findViewById(R.id.tv);
  47.         new Thread(){
  48.             public void run(){
  49.                 try {
  50.                     String localpath=Environment
  51.                     .getExternalStorageDirectory()
  52.                     .getAbsolutePath()+"/CXSOSO/"
  53.                     +"20110901005.3gp";
  54.                     down_file(localpath,"/sdcard/");
  55.                     //下载文件,参数:第一个URL,第二个存放路径

  56.                 } catch (ClientProtocolException e) {
  57.                     // TODO Auto-generated catch block

  58.                     e.printStackTrace();
  59.                 } catch (IOException e) {
  60.                     // TODO Auto-generated catch block

  61.                     e.printStackTrace();
  62.                 }
  63.             }
  64.         }.start();


  65.     }
  66.     public void down_file(String url,String path) throws IOException{
  67.         //下载函数     

  68.         filename=url.substring(url.lastIndexOf("/") + 1);
  69.         //获取文件名

  70.         /*URL myURL = new URL(url);
  71.         URLConnection conn = myURL.openConnection();
  72.         conn.connect();*/
  73.         /*InputStream is = conn.getInputStream();
  74.      this.fileSize = conn.getContentLength();//根据响应获取文件大小
  75. */    
  76.         File file = new File(url);
  77.         DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
  78.         try {
  79.             this.fileSize=read(file).length;
  80.         } catch (Throwable e) {
  81.             // TODO Auto-generated catch block

  82.             e.printStackTrace();
  83.         }
  84.         if (this.fileSize <= 0) throw new RuntimeException("无法获知文件大小 ");
  85.      if (is == null) throw new RuntimeException("stream is null");
  86.      FileOutputStream fos = new FileOutputStream(path+filename);
  87.      //把数据存入路径+文件名

  88.      byte buf[] = new byte[1024];
  89.      downLoadFileSize = 0;
  90.      sendMsg(0);
  91.      do
  92.      {
  93.          //循环读取

  94.      int numread = is.read(buf);
  95.      if (numread == -1)
  96.      {
  97.      break;
  98.      }
  99.      fos.write(buf, 0, numread);
  100.      downLoadFileSize += numread;

  101.      sendMsg(1);//更新进度条

  102.      } while (true);
  103.      sendMsg(2);//通知下载完成

  104.      try
  105.      {
  106.      is.close();
  107.      } catch (Exception ex)
  108.      {
  109.      Log.e("tag", "error: " + ex.getMessage(), ex);
  110.      }

  111.     }
  112.     private void sendMsg(int flag)
  113.     {
  114.      Message msg = new Message();
  115.      msg.what = flag;
  116.      handler.sendMessage(msg);
  117.     }    
  118.      public byte[] read(File myfile) throws Throwable{
  119.             //定义输入流,将文件写入到fpath中

  120.              DataInputStream inStream = new DataInputStream(new BufferedInputStream(new FileInputStream(myfile)));
  121.          byte[] data = http://fxrz12.iteye.com/blog/readFile(inStream);

  122. //         data.length;

  123.          return data;
  124.         }
  125.      public static byte[] readFile(InputStream inStream) throws Throwable{
  126.          int len = 0;
  127.          byte[] buffer = new byte[1024];
  128.          ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  129.          while((len = inStream.read(buffer))!=-1){
  130.          outStream.write(buffer,0,len);
  131.          }
  132.          outStream.close();
  133.          return outStream.toByteArray();
  134.         }

  135. }

阅读(1480) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册