加入两个权限

一个是联网,另一个是读写SD卡

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

下载地址是本人的另外一台主机,现在当服务器了,路径可以测试

http://210.30.12.1:8080/mp3/DJ.mp3

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; public class FileDownProcessBarActivity extends Activity {
/** Called when the activity is first created. */
private static final String Path="http://210.30.12.1:8080/mp3/DJ.mp3";
private ProgressBar progressBar;
private TextView textView;
private Button button;
private int FileLength;
private int DownedFileLength=0;
private InputStream inputStream;
private URLConnection connection;
private OutputStream outputStream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
textView=(TextView) findViewById(R.id.textView2);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
DownedFileLength=0;
// TODO Auto-generated method stub
Thread thread=new Thread(){
public void run(){
try {
DownFile(Path);
} catch (Exception e) {
// TODO: handle exception
}
}
};
thread.start();
}
}
private Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 0:
progressBar.setMax(FileLength);
Log.i("文件长度----------->", progressBar.getMax()+"");
break;
case 1:
progressBar.setProgress(DownedFileLength);
int x=DownedFileLength*100/FileLength;
textView.setText(x+"%");
break;
case 2:
Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_LONG).show();
break; default:
break;
}
}
} }; private void DownFile(String urlString)
{ /*
* 连接到服务器
*/ try {
URL url=new URL(urlString);
connection=url.openConnection();
if (connection.getReadTimeout()==5) {
Log.i("---------->", "当前网络有问题");
// return;
}
inputStream=connection.getInputStream(); } catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*
* 文件的保存路径和和文件名其中Nobody.mp3是在手机SD卡上要保存的路径,如果不存在则新建
*/
String savePAth=Environment.getExternalStorageDirectory()+"/DownFile";
File file1=new File(savePAth);
if (!file1.exists()) {
file1.mkdir();
}
String savePathString=Environment.getExternalStorageDirectory()+"/DownFile/"+"DJ.mp3";
File file =new File(savePathString);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 向SD卡中写入文件,用Handle传递线程
*/
Message message=new Message();
try {
outputStream=new FileOutputStream(file);
byte [] buffer=new byte[1024*4];
FileLength=connection.getContentLength();
message.what=0;
handler.sendMessage(message);
while (DownedFileLength<FileLength) {
outputStream.write(buffer);
DownedFileLength+=inputStream.read(buffer);
Log.i("-------->", DownedFileLength+"");
Message message1=new Message();
message1.what=1;
handler.sendMessage(message1);
}
Message message2=new Message();
message2.what=2;
handler.sendMessage(message2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

Android 下载文件 显示进度条的更多相关文章

  1. VC下载文件显示进度条

    VC下载文件显示进度条 逗比汪星人2009-09-18上传   by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...

  2. VC下载文件 + 显示进度条

    在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...

  3. libcurl开源库在Win32程序中使用下载文件显示进度条实例

    一.配置工程引用libcurl库 #define CURL_STATICLIB #include "curl/curl.h" #ifdef _DEBUG #pragma comme ...

  4. JS原生上传大文件显示进度条-php上传文件

    JS原生上传大文件显示进度条-php上传文件 在php.ini修改需要的大小: upload_max_filesize = 8M    post_max_size = 10M    memory_li ...

  5. 【转】C#中使用aria2c进行下载并显示进度条

    [转自] C#中使用aria2c进行下载并显示进度条 - 云平台知识库 - 博客园https://www.cnblogs.com/littlehb/p/5782714.html 正则表达式的生成网站: ...

  6. android标题栏(titlebar)显示进度条

    在后台线程中执行各种操作(网络连接.大数据存储)的时候,我们希望让客户能看到后台有操作在进行,那么既能有效的提示用户,又不占用当前操作空间,最好的方法就是在标题栏有个进度条. [代码] [Java]代 ...

  7. webclient下载文件 带进度条

    private void button1_Click(object sender, EventArgs e) { doDownload(textBox1.Text.Trim()); } private ...

  8. winform 下载文件显示进度和百分比

    /// <summary> /// 下载完成 /// </summary> private void DownloadFileCompleted() { IsComlate = ...

  9. idhttp post 上传或下载时显示进度条

    通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...

随机推荐

  1. 用JSON-server模拟REST API(二) 动态数据

    用JSON-server模拟REST API(二) 动态数据 上一篇演示了如何安装并运行 json server , 在这里将使用第三方库让模拟的数据更加丰满和实用. 目录: 使用动态数据 为什么选择 ...

  2. sql order by按俩个字段排序

    f1用升序, f2降序,sql该这样写 ORDER BY  f1, f2  DESC 也可以这样写,更清楚: ORDER BY  f1 ASC, f2  DESC 如果都用降序,必须用两个desc O ...

  3. js实现文本框限制输入数字和小数点--兼容多个浏览器

    <html> <head> <meta http-equiv="content-Type" content="text/html;chars ...

  4. glut64位操作系统安装

    64位win7下OpenGL的配置 - walkandthink的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/walkandthink/article/detai ...

  5. PHPStorm+Wamp+Xdebug+Windows7调试代码

    Wamp 集成环境 PHPStorm+Xdebug 调试代码 2013.04.16 花了两个小时时间终于 , 配置成功了 ! 我的开发环境如下 , 其它环境也可以参考我的配置 开发环境 : Windo ...

  6. Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry has been closed.

    现象:更换android studio libs文件夹下的jar包,重新编译代码报错:Cannot locate factory for objects of type DefaultGradleCo ...

  7. Linux多线程编程——多线程与线程同步

    多线程 使用多线程好处: 一.通过为每种事件类型的处理单独分配线程,可以简化处理异步事件的代码,线程处理事件可以采用同步编程模式,启闭异步编程模式简单 二.方便的通信和数据交换 由于进程之间具有独立的 ...

  8. jquery unbind bind

    $(selector).unbind(); --unbind() 方法会删除指定元素的所有事件处理程序 $(selector).unbind("click"); --unbind( ...

  9. 利用FFmpeg生成视频缩略图 2.3.1

    1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...

  10. php的socket通信(二)

    案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...