Android文件下载进度条的实现代码

所属分类: 软件编程 / Android 阅读数: 1473
收藏 0 赞 0 分享
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.
更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多