Android文件下载进度条的实现

我们今天开始学习的是下载进度的实现。今天的这段代码是网上找的,自己做了些小改,通过模拟器测试。文件下载进度条控制(就是为了高清壁纸加个进度条),自己研究了好久,但是进度条只能显示缓存写入文件的进度,不能显示下载进度。找了好久,终于找到一段用的代码,所以记录下来,大家分享。

       布局XML:

Java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView android:id="@+id/tv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text=""
  11. />
  12. <ProgressBar android:id="@+id/down_pb"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:max="100"
  16. style="?android:attr/progressBarStyleHorizontal"
  17. />
  18. </LinearLayout>
复制代码

      这个就不用解释了吧,两个控件,一个是TextView,一个是横向条状进度条

程序main.java:

Java代码:

  1. package eoe.pocketdigi.download;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import org.apache.http.client.ClientProtocolException;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.util.Log;
  13. import android.widget.ProgressBar;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. public class main extends Activity {
  17. /** Called when the activity is first created. */
  18. ProgressBar pb;
  19. TextView tv;
  20. int fileSize;
  21. int downLoadFileSize;
  22. String fileEx,fileNa,filename;
  23. private Handler handler = new Handler()
  24. {
  25. @Override
  26. public void handleMessage(Message msg)
  27. {
  28. //定义一个Handler,用于处理下载线程与UI间通讯
  29. if (!Thread.currentThread().isInterrupted())
  30. {
  31. switch (msg.what)
  32. {
  33. case 0:
  34. pb.setMax(fileSize);
  35. case 1:
  36. pb.setProgress(downLoadFileSize);
  37. int result = downLoadFileSize * 100 / fileSize;
  38. tv.setText(result + "%");
  39. break;
  40. case 2:
  41. Toast.makeText(main.this, "文件下载完成", 1).show();
  42. break;
  43. case -1:
  44. String error = msg.getData().getString("error");
  45. Toast.makeText(main.this, error, 1).show();
  46. break;
  47. }
  48. }
  49. super.handleMessage(msg);
  50. }
  51. };
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.main);
  56. pb=(ProgressBar)findViewById(R.id.down_pb);
  57. tv=(TextView)findViewById(R.id.tv);
  58. new Thread(){
  59. public void run(){
  60. try {
  61. down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");
  62. //下载文件,参数:第一个URL,第二个存放路径
  63. } catch (ClientProtocolException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. }
  71. }.start();
  72. }
  73. public void down_file(String url,String path) throws IOException{
  74. //下载函数
  75. filename=url.substring(url.lastIndexOf("/") + 1);
  76. //获取文件名
  77. URL myURL = new URL(url);
  78. URLConnection conn = myURL.openConnection();
  79. conn.connect();
  80. InputStream is = conn.getInputStream();
  81. this.fileSize = conn.getContentLength();//根据响应获取文件大小
  82. if (this.fileSize <= 0) throw new RuntimeException("无法获知文件大小 ");
  83. if (is == null) throw new RuntimeException("stream is null");
  84. FileOutputStream fos = new FileOutputStream(path+filename);
  85. //把数据存入路径+文件名
  86. byte buf[] = new byte[1024];
  87. downLoadFileSize = 0;
  88. sendMsg(0);
  89. do
  90. {
  91. //循环读取
  92. int numread = is.read(buf);
  93. if (numread == -1)
  94. {
  95. break;
  96. }
  97. fos.write(buf, 0, numread);
  98. downLoadFileSize += numread;
  99. sendMsg(1);//更新进度条
  100. } while (true);
  101. sendMsg(2);//通知下载完成
  102. try
  103. {
  104. is.close();
  105. } catch (Exception ex)
  106. {
  107. Log.e("tag", "error: " + ex.getMessage(), ex);
  108. }
  109. }
  110. private void sendMsg(int flag)
  111. {
  112. Message msg = new Message();
  113. msg.what = flag;
  114. handler.sendMessage(msg);
  115. }
  116. }
复制代码


       大家看了以后就应该明白了,上面写的是用一个循环来完成的这些事情,byte buf[] = new byte[1024];这句话中大家一定要写1024,这个可不能改变呀。sendMsg(0);这句的括号里写的是0,这个也要记得,是0而不是1.

posted @ 2013-01-11 11:41  ggzjj  阅读(646)  评论(0编辑  收藏  举报