我在Dropbox帐户中有多个文件。我正在成功下载文件。但是我希望显示进度条的百分比所以当所有文件都被下载时。进度条结束。我正在使用AsyncTask下载文件。这是我的代码。
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();
}
答案 0 :(得分:4)
在Dropbox API中有一个名为ProgressListener的有用的抽象类,我像这样使用它
DBApi.getFile(dropPath, null, outputStream, new ProgressListener(){
@Override
public long progressInterval() {
return 500;
}
@Override
public void onProgress(long arg0, long arg1) {
int totalSize=(int) arg1;
int downloadedSize=(int) arg0;
//Here you can interact with progressbar
}
});
答案 1 :(得分:2)
有很多样品可供选择。请查看此 tutorial
答案 2 :(得分:0)
您可以使用进度条。代码可能如下所示:
private ProgressBar mProgressBar;
public void onPreExecute(){
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
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();
}
int size = entries.contents.size();
int i = 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());
}
publishProgress(i * 100/size);
i++;
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
mProgressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String result) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
答案 3 :(得分:0)
您需要在doInBackground中调用publishProgress方法。您需要将进度作为publishProgress方法的参数传递。
例如:
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。
您需要像这样处理进度更新
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
}