时间:2021-05-20
android AsyncTask做下载进度条
AsyncTask是个不错的东西,可以使用它来做下载进度条。代码讲解如下:
package com.example.downloadfile; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.widget.TextView; public class DownloadFile extends Activity { public static final String LOG_TAG = "test"; private ProgressDialog mProgressDialog; public static final int DIALOG_DOWNLOAD_PROGRESS = 0; File rootDir = Environment.getExternalStorageDirectory(); //定义要下载的文件名 public String fileName = "test.jpg"; public String fileURL = "https://lh4.googleusercontent.com/-HiJOyupc-tQ/TgnDx1_HDzI/AAAAAAAAAWo/DEeOtnRimak/s800/DSC04158.JPG"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); tv.setText("Android Download File With Progress Bar"); //检查下载目录是否存在 checkAndCreateDirectory("/mydownloads"); //执行asynctask new DownloadFileAsync().execute(fileURL); } class DownloadFileAsync extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); showDialog(DIALOG_DOWNLOAD_PROGRESS); } @Override protected String doInBackground(String... aurl) { try { //连接地址 URL u = new URL(fileURL); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); //计算文件长度 int lenghtOfFile = c.getContentLength(); FileOutputStream f = new FileOutputStream(new File(rootDir + "/my_downloads/", fileName)); InputStream in = c.getInputStream(); //下载的代码 byte[] buffer = new byte[1024]; int len1 = 0; long total = 0; while ((len1 = in.read(buffer)) > 0) { total += len1; //total = total + len1 publishProgress("" + (int)((total*100)/lenghtOfFile)); f.write(buffer, 0, len1); } f.close(); } catch (Exception e) { Log.d(LOG_TAG, e.getMessage()); } return null; } protected void onProgressUpdate(String... progress) { Log.d(LOG_TAG,progress[0]); mProgressDialog.setProgress(Integer.parseInt(progress[0])); } @Override protected void onPostExecute(String unused) { //dismiss the dialog after the file was downloaded dismissDialog(DIALOG_DOWNLOAD_PROGRESS); } } public void checkAndCreateDirectory(String dirName){ File new_dir = new File( rootDir + dirName ); if( !new_dir.exists() ){ new_dir.mkdirs(); } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0 mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Downloading file..."); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(true); mProgressDialog.show(); return mProgressDialog; default: return null; } } }配置文件
注意打开文件保存权限
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android开发使用ProgressBar实现进度条功能。分享给大家供大家参考,具体如下:进度条ProgressBar的使用主要有两种方向;1.
本文实例为大家分享了Android实现垂直进度条的具体代码,供大家参考,具体内容如下水平的进度条见多了,总会想见个垂直的进度条开开眼。今天咱就试试。要说原理也简
本文实例讲述了Android中SeekBar和RatingBar用法。分享给大家供大家参考,具体如下:什么是SeekBar?可以拖动的进度条(在播放器中使用最常
进度条样式在项目中经常可以见到,下面小编给大家分享Android三种方式实现ProgressBar自定义圆形进度条。Android进度条有4种风格可以使用。默认
本文实例为大家分享了Android实现带有进度条按钮效果的具体代码,供大家参考,具体内容如下安卓中带有进度条效果的按钮,如下图:1.布局文件如下activity