大佬教程收集整理的这篇文章主要介绍了Android中使用AsyncTask做下载进度条实例代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 @R_597_10586@l = 0; while ((len1 = in.read(buffer)) > 0) { @R_597_10586@l += len1; //@R_597_10586@l = @R_597_10586@l + len1 publishProgress("" + (int)((@R_597_10586@l*100)/lenghtOfFilE)); f.write(buffer,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; } } }
注意打开文件保存权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://scheR_112_11845@as.android.com/apk/res/android" package="com.example.downloadfile" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@String/app_name"> <activity android:name=".DownloadFile" android:label="@String/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
以上是大佬教程为你收集整理的Android中使用AsyncTask做下载进度条实例代码全部内容,希望文章能够帮你解决Android中使用AsyncTask做下载进度条实例代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。