ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:17.94KB ,
资源ID:6438682      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6438682.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(android文件下载及进度条.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

android文件下载及进度条.docx

1、android 文件下载及进度条Android sdk 3.0 编译通过package com.download;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import .URL;import .URLConnection;import org.apache.http.client.ClientProtocolException;import android.app.Activity;import android.os.Bun

2、dle;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;pu

3、blic class AndroidDownLoadActivity extends Activity private Button DownloadTextButton = null; private Button DownloadMp3Button = null; ProgressBar pb; TextView tv; int fileSize; int downLoadFileSize; String fileEx, fileNa, filename; private Handler handler = new Handler() Override public void handle

4、Message(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(AndroidDownLoad

5、Activity.this, 文件下载完成, 1) .show(); break; case -1: String error = msg.getData().getString(error); Toast.makeText(AndroidDownLoadActivity.this, error, 1) .show(); break; super.handleMessage(msg); ; /* * down load file to sdcard. */ public void down_file(String url, String path) throws IOException / 下

6、载函数 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();/ 根据响应获取文件大小 Log.i(value, length = + this.fileSize); if (this.fileSize

7、= 0) throw new RuntimeException(无法获知文件大小 ); if (is = null) throw new RuntimeException(stream is null); File file = new File(Environment.getExternalStorageDirectory(), filename); FileOutputStream fos = new FileOutputStream(file); / 把数据存入路径+文件名 byte buf = new byte1024; downLoadFileSize = 0; sendMsg(0)

8、; 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 = n

9、ew Message(); msg.what = flag; handler.sendMessage(msg); /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); super.onCreate(savedInstanceState); setContentView(R.layout.main); pb

10、= (ProgressBar) findViewById(R.id.down_pb); tv = (TextView) findViewById(R.id.tv); new Thread() public void run() try down_file( http:/下载路径 , /sdcard/); / 下载文件,参数:第一个URL,第二个存放路径 catch (ClientProtocolException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto

11、-generated catch block e.printStackTrace(); .start(); DownloadTextButton = (Button) findViewById(R.id.DownTextButtonId); DownloadTextButton.setOnClickListener(new DownTextButtonListener(); DownloadMp3Button = (Button) findViewById(R.id.DownMp3ButtonId); DownloadMp3Button.setOnClickListener(new DownM

12、p3ButtonListener(); private class DownTextButtonListener implements OnClickListener public void onClick(View v) Thread thread = new Thread(new Runnable() public void run() / TODO Auto-generated method stub HttpDownloader httpDownloader = new HttpDownloader(); String irc = httpDownloader .download(ht

13、tp:/下载路径 ,); System.out.println(irc); ); thread.start(); private class DownMp3ButtonListener implements OnClickListener public void onClick(View v) Thread thread = new Thread(new Runnable() public void run() / TODO Auto-generated method stub HttpDownloader httpDownloader = new HttpDownloader(); int

14、result = httpDownloader .downFile( http:/下载路径 , test/, love.exe); System.out.println(result); ); thread.start(); package com.download;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import .HttpURLConnection;imp

15、ort .MalformedURLException;import .URL;import .URLConnection;import android.util.Log;public class HttpDownloader private URL url = null; public String download(String urlStr) StringBuffer sb = new StringBuffer(); String line = null; BufferedReader buffer = null; try / 创建一个url对象 url = new URL(urlStr)

16、; / 创建一个Http连接对象 /HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); URLConnection urlConn = url.openConnection(); urlConn.connect(); int fileSize = urlConn.getContentLength();/ 根据响应获取文件大小 Log.d(filesize , * + fileSize); / 使用IO流读取数据 buffer = new BufferedReader(new InputStreamReade

17、r(urlConn .getInputStream(); while (line = buffer.readLine() != null) sb.append(line); catch (Exception e) e.printStackTrace(); finally try buffer.close(); catch (Exception e) e.printStackTrace(); return sb.toString(); public int downFile(String urlStr, String path, String fileName) InputStream inpu

18、tStream = null; try FileUtils fileUtils = new FileUtils(); if (fileUtils.isFileExist(path + fileName) return 1; else inputStream = getInputStreamFromUrl(urlStr); File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream); if (resultFile = null) return -1; catch (Exception e) e.printS

19、tackTrace(); return -1; finally try inputStream.close(); catch (Exception e) e.printStackTrace(); return 0; private InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException / TODO Auto-generated method stub url = new URL(urlStr); HttpURLConnection urlConn = (HttpURL

20、Connection) url.openConnection(); InputStream inputStream = urlConn.getInputStream(); return inputStream; package com.download;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.os.Environment;public cl

21、ass FileUtils private String SDPATH; public String getSDPATH() return SDPATH; public FileUtils() SDPATH = Environment.getExternalStorageDirectory() + /; public File createSDFile(String filename) throws IOException File file = new File(SDPATH + filename); file.createNewFile(); return file; public Fil

22、e createSDDir(String dirname) throws IOException File file = new File(SDPATH + dirname); file.mkdir(); return file; public boolean isFileExist(String fileName) File file = new File(SDPATH + fileName); return file.exists(); public File write2SDFromInput(String path, String fileName, InputStream input

23、) File file = null; OutputStream output = null; try createSDDir(path); file = createSDFile(path + fileName); output = new FileOutputStream(file); byte buffer = new byte4 * 1024; while (input.read(buffer) != -1) output.write(buffer); output.flush(); catch (Exception e) e.printStackTrace(); finally try output.close(); catch (Exception e) e.printStackTrace(); return file; Main.xml Manifest.xml /us

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1