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

Android文件下载进度条的实现代码,第1张

概述main.xml:复制代码代码如下:<?xmlversion=\"1.0\"encoding=\"utf-8\"?><LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\"   android:orientation=\"vertical\"& main.xml:
复制代码 代码如下:@H_404_4@
<?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"
    mce_
/>
</linearLayout>
@H_404_4@
main.java:
复制代码 代码如下:@H_404_4@
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,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);
 } 
}
@H_404_4@
大家看了以后就应该明白了,上面写的是用一个循环来完成的这些事情,byte buf[] = new byte[1024];这句话中大家一定要写1024,这个可不能改变呀。sendMsg(0);这句的括号里写的是0,这个也要记得,是0而不是1. 总结

以上是内存溢出为你收集整理的Android文件下载进度条的实现代码全部内容,希望文章能够帮你解决Android文件下载进度条的实现代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142592.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存