Android文件下载进度条的实现代码
发布时间:2023-02-14 20:08:24
标签:Android,文件下载,进度条
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.
0
投稿
猜你喜欢
- 站点IP访问频率限制 针对单个站点using System;using System.Collections.Generic;u
- 编写C#程序的时候,我们都遇到过配置文件,而如今绝大多数的配置文件都是用XML写的。所以在处理的时候就需要操作XML文件。那么C#如何操作X
- 一、spring定时任务执行两次问题重现和解析最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时
- 题目描述BM99 顺时针旋转矩阵描述 有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。 给定一个NxN的矩阵,和矩阵的阶数N,
- 本文实例为大家分享了Android实现简单旋转动画的具体代码,供大家参考,具体内容如下核心方法public void startAnimat
- BroadcastReceiver(广播 * )是Android中的四大组件之一。下面就具体介绍一下Broadcast Receiver组件
- 1、前言 最近做项目需要用到监测网速及流量,我经过百度和墙内谷歌都没能快速发现监测IPV6流量和网速的用例;也经过自己的一番查询和调试,浪
- 1、Hello, KotlinBugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly 邀请腾讯内部各位技术大咖,通过日常工作经验
- 本文实例讲述了C#中winform控制textbox输入只能为数字的方法。分享给大家供大家参考。具体实现方法如下:添加keyPress事件,
- 本文是Neward & Associates的总裁Ted Neward为developerworks独家撰稿“你不知道5个……”系列
- 大家好,今天尝试用swing技术写一个贪吃蛇大作战小游戏,供大家参考。 效果展示效果展示一、游戏界面二、得分情况&nb
- 一、前言1.1 实现目标服务A调用服务B1和B2(B1和B2提供同种服务),当服务B1/B2在停止和重新发布阶段,或B1/B2有一个服务故障
- 一、介绍在日常的 web 开发中,熟悉 java 的同学一定知道,Spring MVC 可以说是目前最流行的框架,之所以如此的流行,原因很简
- 本文实例为大家分享了C#实现简单文本编辑器的具体代码,供大家参考,具体内容如下建立一个窗体文件,实现对文件的编辑保存和对txt文件的打开界面
- 1、Service的种类按运行地点分类:类别区别 优点缺点 应用本地服务(Local)该服务依附在主进程上,
- 前言之前我们在浅谈6个成员函数中有提到深浅拷贝的问题,现在再回首掏一把。一、深浅拷贝哪家强?先给出代码理一理#define _CRT_SEC
- 复合语句Java的复合语句是以整个区块为单位的语句,由{}以及{}内包含的内容组成对于复合语句来说,复合语句创建了一个局部变量的作用域,该作
- Android 自定义布局竖向的ViewPager的实现效果图:这个自定义控件涉及到的知识点:自定义ViewGroup中onMeasure和
- 找入口对 Spring 有一定基础的同学一定知道,请求入口是DispatcherServlet,所有的请求最终都会落到doDispatch方
- 1. 对象的创建对象创建的主要流程:1.类加载检查虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引