Android-通过更新进度栏下载文件 [英] Android - Downloading File by updating Progress Bar
问题描述
我的保管箱帐户中有多个文件。我已成功下载文件。但是我想显示进度条的百分比,以便下载所有文件时完成。进度条完成。我正在使用AsyncTask下载文件,这是我的代码。
I have multiple files in dropbox account. I am successfully downloading files. But I want to show the progress bar with the percentage so when all files gets downloaded.The progress bar finishes.I am using AsyncTask for downloading files.here is my code.
public void onPreExecute(){
mDialog = new ProgressDialog(mContext);
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setMax(100);
mDialog.show();
}
public void downloadFiles(String filename){
Log.i("Item Name",filename);
File dir = null;
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent){
File sdCard = Environment.getExternalStorageDirectory();
dir = new File (sdCard.getAbsolutePath() + "/AllSecure");
if (!dir.exists()) {
dir.mkdirs();
}
}else{
dir = mContext.getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if(!dir.exists())
{
dir.mkdirs();
}
}
File file = new File(dir, filename);
try {
FileOutputStream mFileOutputStream=new FileOutputStream(file);
DropboxFileInfo mDropboxFileInxfo=mApi.getFile(PHOTO_DIR + filename, null, mFileOutputStream, null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected String doInBackground(String... params) {
SessionUtil ses = new SessionUtil(mContext);
AndroidAuthSession session = ses.buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
Entry entries = null;
try {
System.out.println("mApi is " + mApi);
entries = mApi.metadata(PHOTO_DIR, 10000, null, true, null);
} catch (DropboxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (Entry e : entries.contents) {
if (!e.isDeleted) {
//Log.i("Is Folder",String.valueOf(e.isDir));
downloadFiles(e.fileName());
mFileLen = entries.bytes;
Log.i("Item Name",e.fileName());
}
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(String result) {
mDialog.dismiss();
}
推荐答案
您需要调用 doInBackground中的publishProgress 方法。您需要将进度作为publishProgress方法的参数传递。
You need to call publishProgress method in doInBackground . You need to pass your progress as argument for publishProgress method.
例如:
protected String doInBackground(String... params) {
SessionUtil ses = new SessionUtil(mContext);
AndroidAuthSession session = ses.buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
Entry entries = null;
try {
System.out.println("mApi is " + mApi);
entries = mApi.metadata(PHOTO_DIR, 10000, null, true, null);
} catch (DropboxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int max = entries.contents.size(); // 2 variables need to calculate progress
int current = 0;
for (Entry e : entries.contents) {
if (!e.isDeleted) {
//Log.i("Is Folder",String.valueOf(e.isDir));
downloadFiles(e.fileName());
mFileLen = entries.bytes;
Log.i("Item Name",e.fileName());
current++; //calculate your progress
publishProgress(100 * current / max); //then pass it
}
}
return null;
}
在调用publishProgress之后,将调用onProgressUpdate。
After publishProgress is called onProgressUpdate will be invoked.
您需要像这样处理进度更新
you need to handle progress update like this
protected void onProgressUpdate(Integer... progress) {
Integer p = progress[0];
mDialog.setProgress(p);
mDialog.setMessage(String.format("%d%%", p)); //if you need to see progress percentage in dialog's text
}
这篇关于Android-通过更新进度栏下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!