我使用以下代码从网站下载视频。下载功能正常,我的进度条有一个小问题。 进度条保持不变,并且不会随着值的增加而更新。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFile1 extends AsyncTask<String, Integer, String> {
ProgressDialog bar;
public String videoToDownload;
public String fileName;
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... params) {
int count;
try {
mp4load(videoToDownload);
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
public void mp4load(String urling) {
try {
System.out.println("Downloading");
URL url = new URL(urling);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
//c.setDoOutput(true);
con.connect();
// String downloadsPath = Environment.getExternalStoragePublicDirectory();
File SDCardRoot = Environment.getExternalStorageDirectory();
File outputFile = new File(SDCardRoot, fileName);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(outputFile);
int status = con.getResponseCode();
InputStream is = con.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
System.out.println("Downloaded");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
进度条完全保持0%的进度,并在下载完成后消失。 如何更改进度条的更新部分?
答案 0 :(得分:2)
在doInBackground中调用public class MainController : Controller
{
public ILogger Logger { get; set; }
public IIocResolver IocResolver { get; set; } // Add
public ActionResult Index(string SessionID)
{
Logger.Info("MainController - inside Index");
using (var wrapper = IocResolver.ResolveAsDisposable<RecordManager>(new { sessionID = SessionID })) // Modify
{
var recordMgr = wrapper.Object; // Add
recordMgr.PushRecords();
}
}
}
public class RecordManager : ITransientDependency // Modify
{
public ILogger Logger { get; set; }
private string _sessionID = string.Empty;
public RecordManager(string sessionID, ILogger logger) // Modify
{
_sessionID = sessionID;
Logger = logger; // Modify
Logger.Info("RecordManager - inside constructor");
}
public void PushRecords()
{
Logger.Info("RecordManager - PushRecords - start");
}
}
方法传递一个Integer,在进度条中设置。
publishProgress
您需要将方法mp4load的代码移动到doInBackground,以便像这样修改代码:
protected String doInBackground(String... params) {
int count;
try {
mp4load(videoToDownload);
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
这个想法很简单,您需要获取文件的大小,并在下载文件时慢慢更新进度条。
答案 1 :(得分:1)
参考AsyncTask Docs,您的进度类型参数必须是整数,因为您的AsyncTask类声明如下。
class DownloadFile1 extends AsyncTask<String, Integer, String> {
因此,请更改
public void onProgressUpdate(String..Progress)
到public void onProgressUpdate(Integer..Progress)
然后使用覆盖注释